0
JavaScript Addition
I have this code function MyFunction(){ var Num1 = prompt("Please enter the first number to add together"); var Num2 = prompt("Please enter the seond number to add together"); var Add = Num1 + Num2; document.getElementById("output").innerHTML = Add; } and I want to add the two var's together but instead they add them like they are strings. Is there any way to get by this using prompts or should I use a text box instead? Thank you for any response given.
1 Answer
+ 6
You can convert your numeric string into a number using the Number() function.
var Num1 = Number(prompt("Please enter the first number to add together"));
var Num2 = Number(prompt("Please enter the seond number to add together"));
For more info on the function, refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number