简体   繁体   中英

If doesnt work in Javascript

The If always gives me the last result

check this snippet

 function calc(on, ep, am, ek, b1, b2, b3, b4) { var sum, mo; b1 = parseInt(b1); b2 = parseInt(b2); b3 = parseInt(b3); b4 = parseInt(b4); sum = b1 + b2 + b3 + b4; mo = sum / 4; if (mo < 5) { x = "Not Good"; } if (mo < 6, 5 && mo > 4, 99) { x = "Good"; } if (mo < 8, 5 && mo > 6, 49) { x = "Really Good"; } if (mo > 8, 49) { x = "Perfect"; } alert("O " + on + " " + ep + " " + am + " who is in " + ek + "th semester had avereged " + mo + " " + x); } 
I cant get it to work with else if or something like that but I need it to work however it is

JavaScript floating point literals use a . character (U+002E : FULL STOP) as a decimal point, not a , character (U+002C : COMMA) (which is a comma operator ).

As already said you have to use . and not , . Your mo < 6,5 && mo > 4,99 will always evaluate to 99 which is truthy. So it does not matter what value the mo has.

 var mo = 10; console.log( (mo < 6,5 && mo > 4,99) ); 

See MDN: Comma operator for more informations:

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM