From 4a750a1a0dd1edadfe6eecd144b5940274575bbf Mon Sep 17 00:00:00 2001 From: Alexander Vitshas Date: Sun, 16 May 2021 19:06:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=BA=D1=82=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D0=BA=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=2010.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bjs/07_Number_and_string/index.html | 250 +++++++++++++++++++++++++--- bjs/07_Number_and_string/script.js | 128 +++++++++++++- 2 files changed, 347 insertions(+), 31 deletions(-) diff --git a/bjs/07_Number_and_string/index.html b/bjs/07_Number_and_string/index.html index 645e1c0f..63a61e57 100644 --- a/bjs/07_Number_and_string/index.html +++ b/bjs/07_Number_and_string/index.html @@ -1,32 +1,236 @@ - - + + Калькулятор - - - - - -
-
+ + + + + +
+
-
-
-
- -
-
- -
-
-
+
+
+
+ + +
+
+ +
+ +
+ +
+ +
+ +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
-
- - + + diff --git a/bjs/07_Number_and_string/script.js b/bjs/07_Number_and_string/script.js index 4085abce..578507ac 100644 --- a/bjs/07_Number_and_string/script.js +++ b/bjs/07_Number_and_string/script.js @@ -1,12 +1,124 @@ -let lastOperand = 0; -let operation = null; +let operations = { sum: "+", minus: "-", multi: "*", divide: "/" }; +let history = []; +let allHistory = []; +const PRECISION = 2; -const inputWindow = document.getElementById('inputWindow'); +const inputWindow = document.getElementById("inputWindow"); +const inputHistory = document.getElementById("history"); +const selectHistory = document.getElementById("history_selector"); +renderCalc(); -document.getElementById('btn_clr').addEventListener('click', function () { - lastOperand = 0; - operation = null; - inputWindow.value = ''; -}) +/* Очистка калькулятора */ +document.getElementById("btn_clr").addEventListener("click", function () { + history = []; + renderCalc(); +}); +/* Перерисовка калькулятора */ +function renderCalc() { + inputWindow.value = history.length > 0 ? history[history.length - 1] : "0"; + selectHistory.options[0].text = history.length > 0 ? history.join("") : "0"; +} + +/* Проверка, что последней была введена операция */ +function isLastOperation() { + if (Object.values(operations).indexOf(history[history.length - 1]) >= 0) + return true; + else return false; +} + +/* отмена последнего ввода */ +document.getElementById("btn_clr_last").addEventListener("click", function () { + let isLast = isLastOperation(); + if (history.length) { + if (!isLast) { + let item = history.pop(); + history.push(item.slice(0, item.length - 1)); + } else { + history.pop(); + } + } + renderCalc(); +}); + +/* Нажатие клавиши с операцией */ +Object.keys(operations).forEach((oper) => { + document.getElementById(`btn_${oper}`).addEventListener("click", function () { + if (isLastOperation()) history.pop(); + history.push(operations[oper]); + renderCalc(); + }); +}); + +/* Квадратный корень */ + +document.getElementById("btn_sqrt").addEventListener("click", function () { + if (history.length) { + let isLast = isLastOperation(); + if (!isLast) { + let item = history.pop(); + history.push(Math.sqrt(parseFloat(item)).toPrecision(PRECISION)); + } + } + renderCalc(); +}); + +/* Нажатие цифры на клавиатуре - запись числа в массив history */ +for (let i = 0; i < 10; i++) { + document.querySelector(`#btn_${i}`).addEventListener("click", function (evt) { + if (!history.length) history.push(i.toString()); + else if (isLastOperation()) history.push(i.toString()); + else history.push(history.pop() + i.toString()); + renderCalc(); + }); +} + +/* Нажатие точки */ +document.getElementById("btn_point").addEventListener("click", function () { + if (!isLastOperation() && !history[history.length - 1].match(/\./)) { + history.push(history.pop() + "."); + } + renderCalc(); +}); + +/* Изменение знака */ +document.getElementById("btn_sign").addEventListener("click", function () { + if (!isLastOperation() && history.length) { + history.push((-1 * history.pop()).toString()); + } + renderCalc(); +}); + +/* Вычисление результата */ +document.getElementById("btn_equal").addEventListener("click", function () { + let result = history.reduce((result, current, index, array) => { + if (index === 0) result = parseFloat(current); + let currentIndex = Object.values(operations).indexOf(current); + if (currentIndex >= 0) { + let nextItem = parseFloat(array[index + 1]); + switch (Object.keys(operations)[currentIndex]) { + case "sum": + result += nextItem; + break; + case "minus": + result -= nextItem; + break; + case "multi": + result *= nextItem; + break; + case "divide": + result /= nextItem; + break; + } + } + return result; + }, 0); + history.push("=" + result.toFixed(PRECISION)); + allHistory.push(history); + let newOption = new Option(history.join("")); + selectHistory.append(newOption); + history = []; + history.push(result.toFixed(PRECISION)); + renderCalc(); +});