JavaScript Addition | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
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.

5th Apr 2018, 3:03 PM
Bradley
1 Réponse
+ 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
5th Apr 2018, 3:08 PM
Dev
Dev - avatar