-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_script.js
40 lines (35 loc) · 1.13 KB
/
calculator_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
const screen = document.querySelector('#screen');
const numbers = document.querySelectorAll(".number");
const operators = document.querySelectorAll(".operator");
// to increase the legibility , I collected all of the eventlistener based functions to one function called callEventListeners.
const callEventListeners = ()=>{
numbers.forEach((numberBtn)=>{
numberBtn.addEventListener("click",()=>{
helpers.printNumberToScreen(numberBtn.getAttribute("data-value"));
} )
})
operators.forEach((operatorBtn)=>{
const value = operatorBtn.getAttribute("data-value")
operatorBtn.addEventListener("click",()=>{
if (value == "=") {
helpers.printResultToScreen();
return;
}
if (value == "clear") {
screen.value = null;
return;
}
helpers.printNumberToScreen(value);
} )
})
}
// 'helpers' component help us to find out our similar functions easier. Basically 'a function array'.
const helpers = {
printNumberToScreen : (value) => {
screen.value += value;
},
printResultToScreen : () => {
screen.value = eval(screen.value);
}
}
callEventListeners();