Skip to content Skip to sidebar Skip to footer

How Do I Sum Numbers Using A Prompt, Like A Simple Calculator?

I tried to do a REALLY simple thing using JavaScript, a percentage calculator. This is the code: var num = prompt('What is the number?') var perc = prompt('What is the percentage o

Solution 1:

Yours doesn't work because it's effectively doing "15" + 100 = 15100 because prompt returns a string.

You need to cast it from a string to a number using parseInt

var num1 = parseInt(prompt("Input a number."), 10) //10 for decimalvar num2 = 100alert(num1 + num2)

Post a Comment for "How Do I Sum Numbers Using A Prompt, Like A Simple Calculator?"