简体   繁体   English

Javascript减法返回NaN

[英]Javascript subtraction returns NaN

What is wrong with my code?? 我的代码出了什么问题? Everything except the subtraction works. 除减法之外的所有东西都有效。 It just returns NaN. 它只返回NaN。 I am new to javascript so i might have written my code poorly. 我是javascript的新手,所以我可能写得不好。

// Variables
var count = prompt("Choose an arithmetic method: \n1. Addition \n2.     Subtraktion\n3. Multiplikation\n4. Division");
var x = parseInt(prompt("Enter your first number", "0"));
var y = parseInt(prompt("Enter your second number", "0"));
var z = +x + +y;


// Switch function with 4 cases
switch(count) {
case '1':
alert("Answer: " + z);
break;

case '2':
alert("Answer: " + x - y);
break;

case '3':
alert("Answer: " + x * y);
break;

case '4':
alert("Answer: " + x / y);
break;

} }

You need to group the operations in parentheses, for instance alert("Answer: " + (x - y)); 您需要将操作分组在括号中,例如alert("Answer: " + (x - y)); (and the same for the others). (和其他人一样)。 Otherwise JavaScript runs "Answer: " + x first, resulting in a string. 否则JavaScript首先运行"Answer: " + x ,从而产生一个字符串。

Also, always specify the radix (you want 10) for parseInt: parseInt(input, 10) , otherwise some engines get confused with octal numbers. 另外,总是为parseInt指定基数(你想要10): parseInt(input, 10) ,否则一些引擎会与八进制数混淆。

Your trouble is here: 你的麻烦在这里:

alert("Answer: " + x - y);

Due to how the association of operators work, it works as if you had written this: 由于运算符的关联如何工作,它就好像你写了这样:

alert(("Answer: " + x) - y);

You need to write it like this: 你需要像这样写:

alert("Answer: " + (x - y));

Snippet here: 这里的片段:

 // Variables var count = prompt("Choose an arithmetic method: \\n1. Addition \\n2. Subtraktion\\n3. Multiplikation\\n4. Division"); var x = parseInt(prompt("Enter your first number", "0")); var y = parseInt(prompt("Enter your second number", "0")); var z = +x + +y; // Switch function with 4 cases switch (count) { case '1': alert("Answer: " + z); break; case '2': alert("Answer: " + (x - y)); break; case '3': alert("Answer: " + x * y); break; case '4': alert("Answer: " + x / y); break; } 

Wrap your subtraction in parentheses: (x - y). 用括号括起你的减法:(x - y)。 You would get an unexpected result from the addition, too, if you put x + y there instead of z. 如果你把x + y放在那里而不是z,你也会从添加中得到意想不到的结果。

What's happening is operator precedence. 发生的事情是运营商优先权。 Multiplication and division are higher precedence than addition, so they get done first - before the implicit conversion to string for concatenation. 乘法和除法的优先级高于加法,因此它们首先完成 - 在隐式转换为字符串以进行连接之前。

With subtraction, '+' and '-' are equal in precedence and so get done in order from left to right. 使用减法,“+”和“ - ”在优先级上相等,因此按从左到右的顺序完成。 So the concat takes place before the math, which leaves you attempting to subtract y from a string (which doesn't work, and so...NaN). 因此concat发生在数学之前,这使得你试图从字符串中减去y(这不起作用,因此...... NaN)。

With addition, it would simply concatenate the two numbers onto the string. 另外,它只是将两个数字连接到字符串上。

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

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