Ivan J. Ivan J. - 1 month ago 9
jQuery Question

My if statement "between" is giving me wrong result

I am new here. I have a problem with my if/else if statement.

I got this:

if (unalumno.notas >= "90" && unalumno.notas <= "100") {
unalumno.notas = "A";
} else if (unalumno.notas >= "80" && unalumno.notas <= "89") {
unalumno.notas = "B";
} else if (unalumno.notas >= "70" && unalumno.notas <= "79") {
unalumno.notas = "C";
} else if (unalumno.notas >= "60" && unalumno.notas <= "69") {
unalumno.notas = "D";
} else if (unalumno.notas <= "59") {
unalumno.notas = "F";
}


All the else if statement is giving to me the right result in letters, but the first if continue giving the result in numbers. Hope can understand me. Sorry for english lol

Have a good day

Answer

As I said in my comment, parse the integer out and change all of your comparisons to numbers instead of strings:

var unalumnoNotas = parseInt(unalumno.notas, 10);
if (unalumnoNotas >= 90 && unalumnoNotas <= 100) {
 notas = "A";
} else if (unalumnoNotas >= 80 && unalumnoNotas <= 89) {
 notas = "B";
} else if (unalumnoNotas >= 70 && unalumnoNotas <= 79) {
 notas = "C";
} else if (unalumnoNotas >= 60 && unalumnoNotas <= 69) {
 notas = "D";
} else if (unalumnoNotas <= 59) {
 notas = "F";
}

Also, if you throw an error or return early if the value is greater than 100, you can remove all of the extra && <= 89

if (unalumnoNotas > 100) {
    throw new Error('Number too high');
}

if (unalumnoNotas >= 90) {
 notas = "A";
} else if (unalumnoNotas >= 80) {
 notas = "B";
} else if (unalumnoNotas >= 70) {
 notas = "C";
} else if (unalumnoNotas >= 60) {
 notas = "D";
} else if (unalumnoNotas <= 59) {
 notas = "F";
}