Create a calculator for the entered values.

Write a code that:

  1. Queries the values ​​in turn using the prompt and saves them in an array.
  2. Ends input as soon as the visitor enters a blank line, not a number, or clicks Cancel.

  3. At the same time zero 0 should not finish the input, this is a permitted number.

  4. Displays the sum of all array values.

(function() { var arr = [], res = 0, calc; do { calc = +prompt('Введите число', ''); if (calc == '' || calc == null || isNaN(calc)) break; arr.push(calc); } while (true); for (var i = 0; i < arr.length; i++) { res += arr[i]; } return res; })() 

How to fulfill the third condition ??

    2 answers 2

     (function() { var heap = [], tmp; do{ tmp = prompt('Введите число', ''); // Пусто или null или NaN (0 - это число, так что спокойствие, только спокойствие!) if(tmp === '' || tmp === null || isNaN(tmp)) break; // Если проверка не выбросила из цикла, добавляем в кучу как Number heap.push(+tmp); }while(true); alert(heap.reduce((a, e) => a + e)); })(); 

    • Thank! The only interesting thing is that I also solved it with (===), but for some reason the browser was hanging))? - fara
    • @fara, Hanging on obviously because of a bad cycle, which is potentially infinite. Strict equality cannot hang a tab / browser. And if it can, then it’s right to the Barvin Prize - like Darwin, only in browsers :) - user207618

    Check the entered string before it is cast to a number.

     while ((calc = prompt('Введите число', '') /* присваивание */) && !isNaN(calc)) { calc = +calc; 

    By the way, your option and Cancel does not handle.