-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (99 loc) · 3.07 KB
/
script.js
File metadata and controls
115 lines (99 loc) · 3.07 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
function add(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
return num1 + num2;
}
function minus(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
return num1 - num2;
}
function multiply(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
return num1 * num2;
}
function divide(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
return num1 / num2;
}
let num1;
let num2;
let operator;
let buttonPress;
function operate(num1, operator, num2) {
if (operator === "+") {
return add(num1, num2);
}
if (operator === "-") {
return minus(num1, num2);
}
if (operator === "*") {
return multiply(num1, num2);
}
if (operator === "/") {
return divide(num1, num2);
}
}
const topDisplay = document.querySelector(".calc-top-display");
const display = document.querySelector('.calc-display');
const buttons = document.querySelectorAll('button');
const point = document.querySelector(".btnPoint");
buttons.forEach((button) => {
button.addEventListener('click', () => {
buttonPress = button.value;
if ((buttonPress === "+" || buttonPress === "-" || buttonPress === "*" || buttonPress === "/") && num1 === undefined) {
topDisplay.textContent = display.textContent + buttonPress;
num1 = display.textContent;
operator = buttonPress;
display.textContent = "";
buttonPress = "";
console.log("im in the first if");
}
if ((buttonPress === "+" || buttonPress === "-" || buttonPress === "*" || buttonPress === "/") && num2 === undefined) {
num2 = display.textContent;
console.log(num1);
topDisplay.textContent = roundNum(operate(num1, operator, num2)) + buttonPress;
display.textContent = "";
num1 = operate(num1, operator, num2);
operator = buttonPress;
buttonPress = "";
num2 = undefined;
console.log("Im in the second if statment");
}
if (buttonPress === "=") {
num2 = display.textContent;
display.textContent = roundNum(operate(num1, operator, num2));
num1 = undefined;
num2 = undefined;
operator = undefined;
buttonPress = "";
}
if (button.value === "reset"){
topDisplay.textContent = "";
display.textContent = "";
num1 = undefined;
num2 = undefined;
operator = undefined;
buttonPress = "";
}
if (display.textContent.includes('.')) {
point.disabled = true;
} else {
point.disabled = false;
}
topDisplay.textContent += buttonPress;
display.textContent += buttonPress;
})
})
point.addEventListener('click', () => {
if (display.textContent.includes('.')) {
point.disabled = true;
} else {
point.disabled = false;
}
})
function roundNum(num) {
return Math.round(num * 1000) / 1000;
}