+ 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 ответов
+ 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