-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (74 loc) · 2.54 KB
/
Copy pathscript.js
File metadata and controls
85 lines (74 loc) · 2.54 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
const result = document.getElementById('result');
const colorPicker = document.getElementById('color-picker');
const colorPicker2 = document.getElementById('color-picker2');
const colorPicker1 = document.getElementById('color-picker1');
let total = "";
const section3Buttons = document.querySelector('.section3').children;
colorPicker.addEventListener('change', (event) => {
const color = event.target.value;
for (let i = 0; i < section3Buttons.length; i++) {
section3Buttons[i].style.backgroundColor = color;
}
});
const section2Buttons = document.querySelector('.section2').children;
colorPicker2.addEventListener('change', (event) => {
const color = event.target.value;
for (let i = 0; i < section2Buttons.length; i++) {
section2Buttons[i].style.backgroundColor = color;
}
});
const section1Buttons = document.querySelector('.section1').children;
colorPicker1.addEventListener('change', (event) => {
const color = event.target.value;
for (let i = 0; i < section1Buttons.length; i++) {
section1Buttons[i].style.backgroundColor = color;
}
});
// Section 1: AC, C, /, *
for (let i = 0; i < document.querySelector('.section1').children.length; i++) {
document.querySelector('.section1').children[i].addEventListener('click', (event) => {
const value = event.target.textContent;
if (value === "AC") {
total = "";
result.textContent = "0";
} else if (value === "C") {
total = total.slice(0, -1);
result.textContent = total || "0";
} else if (value === "/" || value === "*") {
total += value;
}
});
}
// Section 2: -, +, ., =
for (let i = 0; i < document.querySelector('.section2').children.length; i++) {
document.querySelector('.section2').children[i].addEventListener('click', (event) => {
const value = event.target.textContent;
if (value === "-" || value === "+") {
total += value;
} else if (value === ".") {
if (!total.includes(".")) {
total += ".";
}
} else if (value === "=") {
total = eval(total);
if (total.toString().length > 12) {
total = "Error";
}
result.textContent = total;
}
});
}
// Section 3: 1234567890()
for (let i = 0; i < document.querySelector('.section3').children.length; i++) {
document.querySelector('.section3').children[i].addEventListener('click', (event) => {
const value = event.target.textContent;
if (value === "(" || value === ")") {
total += value;
} else {
if (total.length < 12) {
total += Number(value);
}
}
result.textContent = total;
});
}