+ 1
Explain please
I try to code simple calculator on JavaScript, here is my first steps to it. Last if block doesn't work, explain please, why? https://code.sololearn.com/WG52lieN7zkK/?ref=app
3 Answers
+ 4
Use Number(a) as it returns a boolean 1 if successful else 0
https://code.sololearn.com/WFC4t4O77vn5/?ref=app
+ 1
Maksim Rukavishnikov
The inputs you receive are of type "string", you need them to be in type number/integer/floats, so that you can add them.
num1 = "20"
num2 = "40"
num1 + num2 = "2040"
So to prevent this, use parseInt()
parseInt(num1) + parseInt(num2)
I would suggest using parseFloat so that it takes floats(decimals) too rather than just simple integers (1,2,3)
0
Thank you!