-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdScript.js
More file actions
115 lines (95 loc) · 3.05 KB
/
Copy patholdScript.js
File metadata and controls
115 lines (95 loc) · 3.05 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//global variables
let firstNumber = '';
let secondNumber = '';
let operator = '';
let display = document.querySelector(".output");
main();
//main function
function main(){
let numbers = document.querySelector(".numpad");
numbers.addEventListener('click', (event)=>{
let target = event.target;
populateDisplay(display, target.textContent);
})
let operators = document.querySelector(".operators");
//if opereator is clicked save display value in first number and
//save clicked operator in operator variable
operators.addEventListener('click', (e)=>{
let target = e.target;
if(!operator){
firstNumber = display.textContent;
display.textContent = ''
operator = target.id;
}
else if(operator){
secondNumber = display.textContent;
display.textContent = ''
firstNumber = operate(firstNumber, operator, secondNumber);
secondNumber = '';
populateDisplay(display, firstNumber)
operator = target.id
}
})
let results = document.querySelector(".end-operations");
results.addEventListener('click', (e)=>{
let target = e.target;
if(target.id === "equal"){
secondNumber = display.textContent;
display.textContent = ''
firstNumber = operate(firstNumber, operator, secondNumber);
secondNumber = '';
populateDisplay(display, firstNumber)
operator = ''
}
else if(target.id === "clear" || firstNumber === "Infinity"){
firstNumber = '';
secondNumber = '';
operator = '';
display.textContent = '';
console.log("clear everyoone")
}
})
}
//functions
//populate display
function populateDisplay(display, content){
if (display.textContent === String(firstNumber)) {
// If the display shows the result of the previous operation,
// clear it before appending the new number
display.textContent = content;
} else {
// Otherwise, append the number to the display
display.textContent += content;
}
}
//operate
function operate(firstNumber, operator, secondNumber){
switch(operator){
case 'add':
return add(firstNumber, secondNumber)
case 'substract':
return substract(firstNumber, secondNumber)
case 'multiply':
return multiply(firstNumber, secondNumber)
case 'divide':
return divide(firstNumber, secondNumber)
}
}
//calculations
function add(firstNum, secondNum){
return Number(firstNum) + Number(secondNum);
}
function substract(firstNum, secondNum){
return Number(firstNum) - Number(secondNum);
}
function multiply(firstNum, secondNum){
return Number(firstNum) * Number(secondNum);
}
function divide(firstNum, secondNum){
if(secondNum!=0){
return Number(firstNum) / Number(secondNum);
}
else{
return "Infinity";
}
}