admin管理员组文章数量:1314514
I am simulating a calculator using Javascript.
Where a user can input 2 numbers in a given text box, and it will display sum, product, difference and division.
Here is my function-
function calculate(num1, num2) {
console.log("First Number = " + num1 + " Second Number = " + num2);
console.log("Sum = " + (num1 + num2));
console.log("Product =" + (num1 * num2));
console.log("Difference = " + (num1 - num2));
console.log("Division = " + (num1 / num2));
}
When user inputs 1st number 4 and second number 3 i get this result:
First Number = 4 Second Number = 3
Sum = 43 // sum should be 7 NOT 43
Product = 12
Difference = 1
Division = 1.3333333333333333
Any ideas what is wrong here?
I am simulating a calculator using Javascript.
Where a user can input 2 numbers in a given text box, and it will display sum, product, difference and division.
Here is my function-
function calculate(num1, num2) {
console.log("First Number = " + num1 + " Second Number = " + num2);
console.log("Sum = " + (num1 + num2));
console.log("Product =" + (num1 * num2));
console.log("Difference = " + (num1 - num2));
console.log("Division = " + (num1 / num2));
}
When user inputs 1st number 4 and second number 3 i get this result:
First Number = 4 Second Number = 3
Sum = 43 // sum should be 7 NOT 43
Product = 12
Difference = 1
Division = 1.3333333333333333
Any ideas what is wrong here?
Share Improve this question asked Oct 10, 2014 at 22:02 AshimaAshima 4,8346 gold badges41 silver badges64 bronze badges 4-
1
They are being treated as strings because you are concatenating them. Try using
parseInt()
. – Goose Commented Oct 10, 2014 at 22:04 - but other operation work fine. It divides correct, subtracts correct, and multiplies correct – Ashima Commented Oct 10, 2014 at 22:05
-
Because
+
is also used to concatenate strings, and it will do that if any part of the equation is a string. – Goose Commented Oct 10, 2014 at 22:06 - Just make sure string is not passed as parameters instead of numbers like calculate('4','3').Your code gives 7 if it is called with calculate(4,3)...It is giving 7 without parseInt for me.Tested in chrome and firefox – AL-zami Commented Oct 11, 2014 at 0:07
4 Answers
Reset to default 6Because + is also a String Concatinater in JavaScript. use parseInt(var1) + parseInt(var2) it will work. also have a look on ---> Javascript Operators
to understand the + operator. Thanks
If you're taking in the numbers in a text box they're treated as strings, so the +
operator will do string concatenation. The *
operator doesn't mean anything in relation to strings, so the javascript engine will try to interpret the inputs as numbers.
You can use parseInt on the inputs to convert them to numbers, or use the html number
input type.
try
console.log("Sum = " + (parseInt(num1) + parseInt(num2)));
or
console.log("Sum = " + (0 + num1 + num2));
also make sure you are calling the function like
calculate(4, 3);
and not
calculate('4', '3');
At least one of your inputs to calculate() is string. + is defined for string so bee 43. While */- are not defined for string and Javascript "cleverly" convert them to int.
本文标签: htmlJavaScriptcannot add 2 numbers correctlyStack Overflow
版权声明:本文标题:html - JavaScript - cannot add 2 numbers correctly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741967555a2407637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论