I\'ve been trying to solve this problem for the last couple days: when I subtract, multiply or divide 2 numbers input through a prompt, everything works fine; but when I wan
try this ,
var a = prompt("Enter first number");
var b = prompt("Enter second number");
var x=parseInt(a);
var y=parseInt(b);
alert(x+y);
When you enter var a and var b, the variables are probably set strings(characters) and not as integers.
So, when you use a + b
, your'e putting those characters together.
For ensuring that the value you enter saves as an integer, you can you use the
parseInt()
For example: var a = parseInt( prompt("Enter first number") );