+ 4

Why does the "+" operator not work?

I made a simple calculator in javascript just to test the waters and for some reason the "+" operator doesn't add the numbers together but instead just outputs them side by side. (Ex: 2 + 2 = 22) I've looked at other people's codes and I've written the "Alert (numx + numy);" just like other people. My initial thought was maybe Alert doesn't support that operator so I tried "document.write (numx + numy);" and got the same result. https://code.sololearn.com/Wfmct110wltw/?ref=app

22nd Aug 2017, 12:05 AM
Timothy Edge
Timothy Edge - avatar
3 odpowiedzi
+ 6
Even though you input numbers in prompt, prompt output it as string. So you need to convert them to numbers using Number or parseInt functions: var numx = Number(prompt("Enter the first number.")); var op = prompt("Enter a simple operator."); var numy = Number(prompt("Enter the second number."));
22nd Aug 2017, 12:53 AM
Calviղ
Calviղ - avatar
+ 3
Thank you!
22nd Aug 2017, 12:56 AM
Timothy Edge
Timothy Edge - avatar
0
Tim me too bro
27th Aug 2017, 12:25 AM
Caleb Henderson
Caleb Henderson - avatar