+ 3
Variable + Variable Javascript [solved]
If I have to variables like this Var first = prompt(âWhat ever numberâ) Var second = prompt(âWhat ever numberâ) document.write(first + second); Input first: 1 second: 1 Output: 11 But the output I want is: 2 How can I fix this?
6 Answers
+ 19
This happens because prompt() returns a string.
Wrap your prompt() calls with this:
Number.parseInt(prompt("..."));
+ 5
Alexander R. Storr-Hansen
It's a common mistake for beginners and the takeaway is user inputs are always treated as string unless specified.
Anyway, since your problem was already solved it would be great if you can mark the answer that you find useful to encourage the community to help each other out. đ
+ 3
Its insane!!! i get the right help from the right people all the time. this is a really great app!! im learning so much. Thanks
+ 3
var first = +prompt(âWhat ever numberâ) // + for convert string to number
var second = +prompt(âWhat ever numberâ)
document.write(first + second);
+ 2
let me check :)
+ 1
sure