-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
36 lines (34 loc) · 1.18 KB
/
scripts.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
const display = document.getElementById('display');
const buttons = Array.from(document.getElementsByTagName('button'));
const clear = document.getElementById('clear');
let currentOperand = '';
let previousOperand = '';
let operation = null;
buttons.map( button => {
button.addEventListener('click', (e) => {
const buttonText = e.target.innerText;
if(buttonText === 'C') {
currentOperand = '';
previousOperand = '';
operation = null;
display.value = '';
return;
}
if(buttonText === '+' || buttonText === '-' || buttonText === '*' || buttonText === '/') {
operation = buttonText;
previousOperand = currentOperand;
currentOperand = '';
return;
}
if(buttonText === '=') {
const calculation = eval(previousOperand + operation + currentOperand);
display.value = calculation;
previousOperand = '';
operation = null;
currentOperand = '';
return;
}
currentOperand = currentOperand.toString() + buttonText.toString();
display.value = currentOperand;
});
});