-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (85 loc) · 2.84 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const calculatorDisplay = document.querySelector("h1");
const inputBtns = document.querySelectorAll("button");
const clearBtn = document.getElementById("clear-btn");
const tipsBtns = document.querySelectorAll(".tip");
let firstValue = 0;
let operatorValue = "";
let awaitingNextValue = false;
let tipsValue = 0;
function sendNumberValue(number) {
//replace current display if first entered
if (awaitingNextValue) {
calculatorDisplay.textContent = number;
awaitingNextValue = false;
} else {
//if current display value is 0,replace it or add number
const displayValue = calculatorDisplay.textContent;
calculatorDisplay.textContent =
displayValue === "0" ? number : displayValue + number;
}
}
//add decimal
function addDecimal() {
//if operator pressed ,don't add decimal
if (awaitingNextValue) return;
//if no add one
if (!calculatorDisplay.textContent.includes(".")) {
calculatorDisplay.textContent = `${calculatorDisplay.textContent}.`;
}
}
//calculate first and second values
const calculate = {
"/": (firstNumber, secondNumber) => firstNumber / secondNumber,
"*": (firstNumber, secondNumber) => firstNumber * secondNumber,
"+": (firstNumber, secondNumber) => firstNumber + secondNumber,
"-": (firstNumber, secondNumber) => firstNumber - secondNumber,
"=": (firstNumber, secondNumber) => secondNumber,
};
//operatore
function useOperator(operator) {
const currentValue = Number(calculatorDisplay.textContent);
//prevent multiple operators
if (operatorValue && awaitingNextValue) {
operatorValue = operator;
return;
}
//addign firstvalue if no
if (!firstValue) {
firstValue = currentValue;
} else {
const calculation = calculate[operatorValue](firstValue, currentValue);
console.log("calculation", calculation);
calculatorDisplay.textContent = calculation;
firstValue = calculation;
}
//ready for next value
awaitingNextValue = true;
operatorValue = operator;
}
//add event listener
inputBtns.forEach((inputBtn) => {
if (inputBtn.classList.length === 0) {
inputBtn.addEventListener("click", () => sendNumberValue(inputBtn.value));
} else if (inputBtn.classList.contains("operator")) {
inputBtn.addEventListener("click", () => useOperator(inputBtn.value));
} else if (inputBtn.classList.contains("decimal")) {
inputBtn.addEventListener("click", () => addDecimal());
}
});
//reset display
function resetAll() {
firstValue = 0;
operatorValue = "";
awaitingNextValue = false;
calculatorDisplay.textContent = "0";
}
//calculate tips
function calculateTips(percentage) {
tipsValue = (Number(calculatorDisplay.textContent) * (1 + percentage)) / 100;
calculatorDisplay.textContent = tipsValue;
}
//event listener
clearBtn.addEventListener("click", resetAll);
tipsBtns.forEach((tipsBtn) => {
tipsBtn.addEventListener("click", () => calculateTips(tipsBtn.value));
});