work
This commit is contained in:
parent
776e83e1f1
commit
480e69b843
87 changed files with 1020 additions and 233 deletions
27
1-js/4-data-structures/6-array/5-array-input-sum/solution.md
Normal file
27
1-js/4-data-structures/6-array/5-array-input-sum/solution.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
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