work
This commit is contained in:
parent
1bffa43db4
commit
057783d216
373 changed files with 203 additions and 190 deletions
|
@ -1,27 +0,0 @@
|
|||
Please note the subtle, but important detail of the solution. We don't convert `value` to number instantly after `prompt`, because after `value = +value` we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
|
||||
|
||||
|
||||
```js run demo
|
||||
function sumInput() {
|
||||
|
||||
let numbers = [];
|
||||
|
||||
while (true) {
|
||||
|
||||
let value = prompt("A number please?", 0);
|
||||
|
||||
// should we cancel?
|
||||
if (value === "" || value === null || !isFinite(value)) break;
|
||||
|
||||
numbers.push(+value);
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (let number of numbers) {
|
||||
sum += number;
|
||||
}
|
||||
}
|
||||
|
||||
alert( sumInput() );
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue