Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Циклический калькулятор (https://javascript.ru/forum/misc/68187-ciklicheskijj-kalkulyator.html)

maks_Kraevoj 31.03.2017 23:14

Циклический калькулятор
 
Почему не работает вот таакой калькулятор?

do {
        function calculator() {


            function add(a, b) {
                return a + b;
            }

            function sub(a, b) {
                return a - b;
            }

            function mul(a, b) {
                return a * b;
            }

            function div(a, b) {
                return a / b;
            }

            var operand1 = prompt("Введите первое число: ");
            var sign = prompt("Введите знак арифметической операции: + - * /");
            var operand2 = prompt("Введите второе число: ");
            var result;

            operand1 = parseInt(operand1);
            operand2 = parseInt(operand2);

            switch (sign) {
                case "+":
                    result = add(operand1, operand2);
                    break;
                case "-":
                    result = sub(operand1, operand2);
                    break;
                case "*":
                    result = mul(operand1, operand2);
                    break;
                case "/":
                    result = div(operand1, operand2);
                    break;

                default:
                    document.write("<p><b style='color:red'>" + sign + "</b> - не является знаком арифметической операции.");
            }


            document.write("<p>" + operand1 + " " + sign + " " + operand2 + " = " + result);
        }
        setTimeout(calculator, 5000);
    } while (calculator);


Мне надо:

1. человек вводит что он хочет сделать (умножить, разделить и т.д.)
2. Вводит 1-вое число
3. Вводит 2-рое число
4. Получает результат через document.write на самой странице
5. Потом идет задержка(ну скажем секунд 5)
6. После опять пункт 1 и т.д.

рони 01.04.2017 00:04

maks_Kraevoj,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">


</head>

<body>

  <script>
window.addEventListener("DOMContentLoaded", function() {

        !function calculator() {


            function add(a, b) {
                return a + b;
            }

            function sub(a, b) {
                return a - b;
            }

            function mul(a, b) {
                return a * b;
            }

            function div(a, b) {
                return a / b;
            }

            var operand1 = prompt("Введите первое число: ");
            var sign = prompt("Введите знак арифметической операции: + - * /");
            var operand2 = prompt("Введите второе число: ");
            var result, html = "<p><b style='color:red'>" + sign + "</b> - не является знаком арифметической операции.";

            operand1 = parseInt(operand1);
            operand2 = parseInt(operand2);

            switch (sign) {
                case "+":
                    result = add(operand1, operand2);
                    break;
                case "-":
                    result = sub(operand1, operand2);
                    break;
                case "*":
                    result = mul(operand1, operand2);
                    break;
                case "/":
                    result = div(operand1, operand2);
                    break;

                default:
                   result = null
            }
          if(result !== null) html = "<p>" + operand1 + " " + sign + " " + operand2 + " = " + result;
          document.body.insertAdjacentHTML("beforeEnd", html);
           setTimeout(calculator, 5000);

        }()



});
  </script>

</body>
</html>


Часовой пояс GMT +3, время: 09:32.