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?

23rd Jul 2023, 3:32 PM
𝒩 ℛ
𝒩 ℛ - avatar
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); };
24th Jul 2023, 6:37 PM
^^^WILL_gamerz^^^
^^^WILL_gamerz^^^ - avatar
+ 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.
23rd Jul 2023, 4:54 PM
Yasin Rahnaward
Yasin Rahnaward - avatar
+ 5
You didn't invoke the add function. add()
23rd Jul 2023, 4:09 PM
ODLNT
ODLNT - avatar
+ 2
Thanks!🙏👍
25th Jul 2023, 8:32 AM
𝒩 ℛ
𝒩 ℛ - avatar