0
JavaScript problem
Why this doesn't works đ var Input1 = prompt("Please enter the first input:",""); var Input2 = prompt("Please enter the second input:",""); var answer = Input1 + Input2; function add() { alert("The answer is: " + answer); }; Where's the problem?
4 Answers
+ 1
Just convert the prompt from string to Number ..
This is the solution by using Number() function to make any string to number that is supported....
Here is the code :
var Input1 = Number(prompt("Please enter the first input:",""));
var Input2 = Number(prompt("Please enter the second input:",""));
var answer = Input1 + Input2;
function add() {
alert("The answer is: " + answer);
};
+ 7
In addition to ODLNT ,Prompt function can take two parameter, (message, default value [string]), so if you want to calculate sum of two numbers before anything it's important to convert the input values to numbers. Else if you want to concatenate two string it's all find.
+ 5
You didn't invoke the add function.
add()
+ 2
Thanks!đđ