en.javascript.info/1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
MuhammedZakir 0f5b63d86f Restructure the Solution for 'Army of Functions' task and Fix Typos
Fix #2068 - Army of Functions
Fix #2070 - Typo
Fix #2056 - Grammatical Error
Fix #2074 - Remove semi-colon after function declaration
2020-09-04 13:38:41 +05:30

33 lines
717 B
Markdown

The reason is that prompt returns user input as a string.
So variables have values `"1"` and `"2"` respectively.
```js run
let a = "1"; // prompt("First number?", 1);
let b = "2"; // prompt("Second number?", 2);
alert(a + b); // 12
```
What we should do is to convert strings to numbers before `+`. For example, using `Number()` or
prepending them with `+`.
For example, right before `prompt`:
```js run
let a = +prompt("First number?", 1);
let b = +prompt("Second number?", 2);
alert(a + b); // 3
```
Or in the `alert`:
```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
alert(+a + +b); // 3
```
Using both unary and binary `+` in the latest code. Looks funny, doesn't it?