-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented calculator functionality
Added input validation, operand and result length limits, error handling, and decimal precision
- Loading branch information
1 parent
0cc9756
commit f5f8391
Showing
2 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,238 @@ | ||
"use strict"; | ||
const buttons = document.querySelector("#button-container"); | ||
const MAX_INPUT_LENGTH = 13; | ||
const MAX_RESULT_LENGTH = 14; | ||
let calculator = { | ||
firstOperand: "", | ||
operator: "", | ||
secondOperand: "", | ||
result: "", | ||
}; | ||
buttons.addEventListener("click", (event) => { | ||
const target = event.target; | ||
switch (target.id) { | ||
case "btn-zero": | ||
inputBtnClicked("0"); | ||
break; | ||
case "btn-one": | ||
inputBtnClicked("1"); | ||
break; | ||
case "btn-two": | ||
inputBtnClicked("2"); | ||
break; | ||
case "btn-three": | ||
inputBtnClicked("3"); | ||
break; | ||
case "btn-four": | ||
inputBtnClicked("4"); | ||
break; | ||
case "btn-five": | ||
inputBtnClicked("5"); | ||
break; | ||
case "btn-six": | ||
inputBtnClicked("6"); | ||
break; | ||
case "btn-seven": | ||
inputBtnClicked("7"); | ||
break; | ||
case "btn-eight": | ||
inputBtnClicked("8"); | ||
break; | ||
case "btn-nine": | ||
inputBtnClicked("9"); | ||
break; | ||
case "btn-dot": | ||
inputBtnClicked("."); | ||
} | ||
switch (target.id) { | ||
case "btn-add": | ||
addBtnClicked(); | ||
break; | ||
case "btn-subtract": | ||
subtractBtnClicked(); | ||
break; | ||
case "btn-multiply": | ||
multiplyBtnClicked(); | ||
break; | ||
case "btn-divide": | ||
divideBtnClicked(); | ||
break; | ||
case "btn-percentage": | ||
percentageBtnClicked(); | ||
break; | ||
case "btn-equals": | ||
equalsBtnClicked(); | ||
break; | ||
} | ||
}); | ||
function inputBtnClicked(input) { | ||
if (calculator.result !== "") { | ||
calculator.result = ""; | ||
} | ||
if (calculator.operator == "") { | ||
if (calculator.firstOperand.length == MAX_INPUT_LENGTH) { | ||
handleError("InputLengthExceeded"); | ||
return; | ||
} | ||
if (input === "." && calculator.firstOperand.includes(".")) { | ||
return; | ||
} | ||
calculator.firstOperand += input; | ||
} | ||
else { | ||
if (calculator.firstOperand.length == MAX_INPUT_LENGTH) { | ||
handleError("InputLengthExceeded"); | ||
return; | ||
} | ||
if (input === "." && calculator.secondOperand.includes(".")) { | ||
return; | ||
} | ||
calculator.secondOperand += input; | ||
} | ||
} | ||
function addBtnClicked() { | ||
if (calculator.firstOperand !== "" && calculator.secondOperand == "" && calculator.result == "") { | ||
calculator.operator = "+"; | ||
} | ||
else if (calculator.firstOperand == "" && calculator.secondOperand == "" && calculator.result !== "") { | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "+"; | ||
} | ||
else { | ||
operate(); | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "+"; | ||
} | ||
} | ||
function subtractBtnClicked() { | ||
if (calculator.firstOperand !== "" && calculator.secondOperand == "" && calculator.result == "") { | ||
calculator.operator = "-"; | ||
} | ||
else if (calculator.firstOperand == "" && calculator.secondOperand == "" && calculator.result !== "") { | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "-"; | ||
} | ||
else { | ||
operate(); | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "-"; | ||
} | ||
} | ||
function multiplyBtnClicked() { | ||
if (calculator.firstOperand !== "" && calculator.secondOperand == "" && calculator.result == "") { | ||
calculator.operator = "*"; | ||
} | ||
else if (calculator.firstOperand == "" && calculator.secondOperand == "" && calculator.result !== "") { | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "*"; | ||
} | ||
else { | ||
operate(); | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "*"; | ||
} | ||
} | ||
function divideBtnClicked() { | ||
if (calculator.firstOperand !== "" && calculator.secondOperand == "" && calculator.result == "") { | ||
calculator.operator = "/"; | ||
} | ||
else if (calculator.firstOperand == "" && calculator.secondOperand == "" && calculator.result !== "") { | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "/"; | ||
} | ||
else { | ||
operate(); | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.operator = "/"; | ||
} | ||
} | ||
function percentageBtnClicked() { | ||
if (calculator.firstOperand !== "" && calculator.secondOperand == "" && calculator.result == "") { | ||
calculator.firstOperand = `${percentage(calculator.firstOperand)}`; | ||
} | ||
else { | ||
calculator.firstOperand = calculator.result; | ||
calculator.result = ""; | ||
calculator.firstOperand = `${percentage(calculator.firstOperand)}`; | ||
} | ||
} | ||
function equalsBtnClicked() { | ||
if (calculator.firstOperand == "" && | ||
calculator.secondOperand == "" && | ||
calculator.operator == "" && | ||
calculator.result == "") { | ||
return handleError("EmptyCalculatorObject"); | ||
} | ||
else { | ||
operate(); | ||
} | ||
} | ||
function add(a, b) { | ||
return parseFloat((+a + +b).toFixed(10)); | ||
} | ||
function subtract(a, b) { | ||
return parseFloat((+a - +b).toFixed(10)); | ||
} | ||
function multiply(a, b) { | ||
return parseFloat((+a * +b).toFixed(10)); | ||
} | ||
function divide(a, b) { | ||
if (b == 0 || b == "0") { | ||
handleError("DivisionByZero"); | ||
return; | ||
} | ||
return parseFloat((+a / +b).toFixed(10)); | ||
} | ||
function percentage(a) { | ||
return +a / 100; | ||
} | ||
function operate() { | ||
switch (calculator.operator) { | ||
case "+": | ||
calculator.result = `${add(calculator.firstOperand, calculator.secondOperand)}`; | ||
calculator.firstOperand = ""; | ||
calculator.secondOperand = ""; | ||
calculator.operator = ""; | ||
break; | ||
case "-": | ||
calculator.result = `${subtract(calculator.firstOperand, calculator.secondOperand)}`; | ||
calculator.firstOperand = ""; | ||
calculator.secondOperand = ""; | ||
calculator.operator = ""; | ||
break; | ||
case "*": | ||
calculator.result = `${multiply(calculator.firstOperand, calculator.secondOperand)}`; | ||
calculator.firstOperand = ""; | ||
calculator.secondOperand = ""; | ||
calculator.operator = ""; | ||
break; | ||
case "/": | ||
calculator.result = `${divide(calculator.firstOperand, calculator.secondOperand)}`; | ||
calculator.firstOperand = ""; | ||
calculator.secondOperand = ""; | ||
calculator.operator = ""; | ||
break; | ||
} | ||
if (calculator.result.length > MAX_RESULT_LENGTH) { | ||
handleError("ResultLengthExceeded"); | ||
} | ||
} | ||
function handleError(error) { | ||
error == "InputLengthExceeded" | ||
? console.log("LENGTH EXCEEDED!!") | ||
: error == "DivisionByZero" | ||
? console.log("Cannot divide by zero!") | ||
: error == "EmptyCalculatorObject" | ||
? console.log("Enter a pair of numbers first!") | ||
: error == "ResultLengthExceeded" | ||
? console.log("Cannot calculate!") | ||
: console.log("ERROR!"); | ||
} |
Oops, something went wrong.