|
| 1 | +// default values |
| 2 | + |
| 3 | +let operator = ""; |
| 4 | +let previousValue = ""; |
| 5 | +let currentValue = ""; |
| 6 | + |
| 7 | +// html dom into javascript |
| 8 | + |
| 9 | +const clearButton = document.querySelector("#clear"); |
| 10 | +const deleteButton = document.querySelector("#delete"); |
| 11 | +const numberButton = document.querySelectorAll(".number"); |
| 12 | +const operatorButton = document.querySelectorAll(".operator"); |
| 13 | +const currentScreen = document.querySelector(".bottomrow"); |
| 14 | +const previousScreen = document.querySelector(".toprow"); |
| 15 | +const equalNumber = document.querySelector(".equal"); |
| 16 | + |
| 17 | +// display number when one is clicked to current screen |
| 18 | + |
| 19 | +numberButton.forEach((numberClick) => |
| 20 | + numberClick.addEventListener("click", function (e) { |
| 21 | + handleNumber(e.target.textContent); |
| 22 | + currentScreen.textContent = currentValue; |
| 23 | + }) |
| 24 | +); |
| 25 | + |
| 26 | +// display operator to previous screen along with previous value |
| 27 | + |
| 28 | +operatorButton.forEach((operatorClick) => |
| 29 | + operatorClick.addEventListener("click", function (e) { |
| 30 | + handleOperator(e.target.textContent); |
| 31 | + previousScreen.textContent = previousValue + " " + operator; |
| 32 | + currentScreen.textContent = currentValue; |
| 33 | + }) |
| 34 | +); |
| 35 | + |
| 36 | +// number function, if current value is <=5 add a number digit |
| 37 | + |
| 38 | +function handleNumber(number) { |
| 39 | + if (currentValue.length <= 5) { |
| 40 | + currentValue += number; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// operator function |
| 45 | + |
| 46 | +function handleOperator(operatorClick) { |
| 47 | + operator = operatorClick; |
| 48 | + previousValue = currentValue; |
| 49 | + currentValue = ""; |
| 50 | +} |
| 51 | + |
| 52 | +// clear button, makes operator and values blank followed by the screens blank |
| 53 | + |
| 54 | +clearButton.addEventListener("click", () => { |
| 55 | + operator = ""; |
| 56 | + previousValue = ""; |
| 57 | + currentValue = ""; |
| 58 | + previousScreen.textContent = currentValue; |
| 59 | + currentScreen.textContent = currentValue; |
| 60 | +}); |
| 61 | + |
| 62 | +// delete button, removes one digit from current screen and stores it as current value |
| 63 | + |
| 64 | +deleteButton.addEventListener("click", function deleteNumber() { |
| 65 | + currentScreen.textContent = currentScreen.textContent.slice(0, -1); |
| 66 | + currentValue = currentScreen.textContent; |
| 67 | +}); |
| 68 | + |
| 69 | +// equal button, doesn't do anything if current value and previous screen are blank. runs operate and round number functions |
| 70 | + |
| 71 | +equalNumber.addEventListener("click", function () { |
| 72 | + if (currentValue != "" && previousScreen != "") { |
| 73 | + previousScreen.textContent = |
| 74 | + previousValue + " " + operator + " " + currentValue + " " + "="; |
| 75 | + operate(); |
| 76 | + roundNumber(); |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | +// operate function, changes values from strings to numbers, then +-x/ previous value by current value |
| 81 | + |
| 82 | +function operate() { |
| 83 | + previousValue = Number(previousValue); |
| 84 | + currentValue = Number(currentValue); |
| 85 | + |
| 86 | + if (operator === "+") { |
| 87 | + previousValue += currentValue; |
| 88 | + } else if (operator === "-") { |
| 89 | + previousValue -= currentValue; |
| 90 | + } else if (operator === "x") { |
| 91 | + previousValue *= currentValue; |
| 92 | + } else { |
| 93 | + previousValue /= currentValue; |
| 94 | + } |
| 95 | + currentScreen.textContent = roundNumber(previousValue); |
| 96 | +} |
| 97 | + |
| 98 | +// rounds previous value to 3 decimal places |
| 99 | + |
| 100 | +function roundNumber(previousValue) { |
| 101 | + return Math.round(previousValue * 1000) / 1000; |
| 102 | +} |
0 commit comments