-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
76 lines (66 loc) · 3.4 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
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
/*Program Variables ====================
=====================================*/
//Variables that Hold DOM ELEMENTS
const terminal = document.getElementById('terminal');
const buttonsContainer = document.querySelector('.buttons-container');
const numberButtons = document.querySelectorAll('.number');
//Variables the 3 elemenst to build an expression with the calculator
let firstOperand = '';
let secondOperand = '';
let operator = '';
/* Start of Program =============================
=============================================== */
// Event Handler delegated to the parent div.buttons-container = buttonsContainer
buttonsContainer.addEventListener('click', (e) =>{
if(e.target.className == 'number'){ //if the button holds just a number
terminal.textContent += e.target.textContent; //add the text of that button to the terminal
firstOperand += e.target.textContent; // add the text of that button to the firstOperand
}
if(e.target.className == 'operator'){ //if the button holds an operator
terminal.textContent += e.target.textContent; // add the text of that button to the terminal
operator = e.target.textContent; //set the operator equals to the text of the button
numberButtons.forEach(function(item){ // change class attribute of every number button to .number-two
item.className = 'number-two';
})
}
if(e.target.className == 'number-two'){ //if the button has class number-two
terminal.textContent += e.target.textContent; //add the text of that button to the terminal
secondOperand += e.target.textContent; //add the text of that button to the secondOperator
}
if(e.target.id == 'equal'){
let result = '';
//When you press the equals button, if opeartor is +, return the sum of the first and second operands
if(operator == '+'){
result = parseFloat(firstOperand) + parseFloat(secondOperand);
}
//When you press the equals button, if opeartor is - , return the subtraction of the first operands minus the second operand
if(operator == '-'){
result = parseFloat(firstOperand) - parseFloat(secondOperand);
}
//When you press the equals button, if opeartor is * , return the moltiplication of the first operands by the second operand
if(operator == 'x'){
result = parseFloat(firstOperand) * parseFloat(secondOperand);
}
//When you press the equals button, if opeartor is / , return the division of the first operands by the second operand
if(operator == '/'){
result = parseFloat(firstOperand) / parseFloat(secondOperand);
}
//Display the result on the terminal
terminal.textContent = result;
}
//if you press the clear button
if(e.target.id == 'clear'){
//reset text content of terminal,
terminal.textContent = '';
// reset firstOperand,
firstOperand = '';
// reset secondoperand
secondOperand = '';
// reset operator
operator = '';
// reset the class of each number button to 'number';
numberButtons.forEach(function(item){
item.className = 'number';
})
}
});