简体   繁体   English

如果在Javascript中不起作用

[英]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 . JavaScript 浮点文字使用. character (U+002E : FULL STOP) as a decimal point, not a , character (U+002C : COMMA) (which is a comma operator ). 字符(U + 002E:FULL STOP),为小数点,而不是一个,字符(U + 002C:COMMA)(其是逗号运算 )。

As already said you have to use . 如前所述,您必须使用. and not , . 而不是, Your mo < 6,5 && mo > 4,99 will always evaluate to 99 which is truthy. 您的mo < 6,5 && mo > 4,99将始终计算为99 ,这是事实。 So it does not matter what value the mo has. 因此, mo具有什么价值都没有关系。

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

See MDN: Comma operator for more informations: 有关更多信息,请参见MDN:逗号运算符

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand. 逗号运算符评估每个操作数(从左到右)并返回最后一个操作数的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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