Skip to content

Commit

Permalink
182. Code Review
Browse files Browse the repository at this point in the history
  • Loading branch information
bergstenarn committed Nov 16, 2021
1 parent 05da682 commit 26c6c92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const calculatorDisplay = document.querySelector("h1");
const inputBtns = document.querySelectorAll("button");
const clearBtn = document.getElementById("clear-btn");

// Calculate first and second values depending on operator
const calculate = {
"/": (firstNumber, secondNumber) => firstNumber / secondNumber,
"*": (firstNumber, secondNumber) => firstNumber * secondNumber,
"+": (firstNumber, secondNumber) => firstNumber + secondNumber,
"-": (firstNumber, secondNumber) => firstNumber - secondNumber,
"=": (firstNumber, secondNumber) => secondNumber,
};

let firstValue = 0;
let operatorValue = "";
let awaitingNextValue = false;
Expand All @@ -27,15 +36,6 @@ function addDecimal() {
}
}

// Calculate first and second values depending on operator
const calculate = {
"/": (firstNumber, secondNumber) => firstNumber / secondNumber,
"*": (firstNumber, secondNumber) => firstNumber * secondNumber,
"+": (firstNumber, secondNumber) => firstNumber + secondNumber,
"-": (firstNumber, secondNumber) => firstNumber - secondNumber,
"=": (firstNumber, secondNumber) => secondNumber,
};

function useOperator(operator) {
const currentValue = Number(calculatorDisplay.textContent);
// Prevent multiple operators
Expand All @@ -56,6 +56,14 @@ function useOperator(operator) {
operatorValue = operator;
}

// Reset display
function resetAll() {
firstValue = 0;
operator = "";
awaitingNextValue = false;
calculatorDisplay.textContent = "0";
}

// Add event listeners for numbers, operators and decimal
inputBtns.forEach((inputBtn) => {
if (inputBtn.classList.length === 0) {
Expand All @@ -67,13 +75,5 @@ inputBtns.forEach((inputBtn) => {
}
});

// Reset display
function resetAll() {
firstValue = 0;
operator = "";
awaitingNextValue = false;
calculatorDisplay.textContent = "0";
}

// Event listener
clearBtn.addEventListener("click", resetAll);
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ body {
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom, #f12711, #f5af19);
background: linear-gradient(to bottom, #677dbb, #c5c21e);
}

.calculator {
Expand Down

0 comments on commit 26c6c92

Please sign in to comment.