I'm trying to apply a medical score in a web application. The search function didn't provide me satisfactory results.
A part of the score is not mathematically but mere logically determined.
It needs three variables which values are inspected and scored.
Scoring works like this:
Score Grades
0 <-- All 0
1 <-- At least one 1, but no >1
2 <-- 2 in only one region (respectively the variable)
3 <-- 2 in more than one region (respectively the variable)
4 <-- 3 in one or more region (respectively the variable)
5 <-- 4 in only one region (respectively the variable)
6 <-- 4 in more than one region (respectively the variable)
Last year I developed a Python script that was functional but I wanted to implement this score via javaScript into a web app. I came up with a counting function - to count how many times a certain number appears.
arr = [];
var a = 2;
var b = 4;
var c = 0;
arr.push(a, b, c);
function countThis(numberToCount) {
count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
}
These lines seem to work. But now comes the part that bugs me.
I set up a bunch of conditions - all of them chained and combined with the function above - to calculate the score.
function scoreArr() {
score = 0;
if (a === 0 && b === 0 && c === 0) {
score = 0;
} else if (a <= 1 && b <= 1 && c <= 1 && countThis(1) >= 1) {
score = 1;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) >= 2) {
score = 6;
} else if (a <= 4 || b <= 4 || c <= 4 && countThis(4) == 1) {
score = 5;
} else if (a <= 3 || b <= 3 || c <= 3 && countThis(3) >= 1) {
score = 4;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) > 1) {
score = 3;
} else if (a <= 2 || b <= 2 || c <= 2 && countThis(2) == 1) {
score = 2;
}
return score;
}
a = 0, b = 0, c = 0 gives the correct score of 0.
But with every other possible combination (numbers 0 to 4) I get the exact same result: 6. Which is clearly wrong.
I tried to change the operators, added parenthesis, ... but nothing made it work. I think that the problem is hidden (?) in my conditions, but I don't know how to fix it.
Here is the JSFIDDLE.
I must admit that I'm pretty new to coding javaScript. So if anybody can explain to me what I did wrong, I would be very grateful.
Thank you very much for your time.