-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (42 loc) · 1.12 KB
/
app.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
const ZERO = 0;
// set initial count
let count = ZERO;
// select value and buttons
/**
* We are using '#' because we are fetching and id-->value
*/
const value = document.querySelector("#value");
const btns = document.querySelectorAll(".btn");
const click = "click";
// console.log(value);
// console.log(btns);
// Instead of calling AddEventListener for each button one by one,
// we will use forEach() method instead.
btns.forEach(function (btn) {
btn.addEventListener(click, function (eventObject) {
const blackColor = "#222";
const greenColor = "green";
const redColor = "red";
const styles = eventObject.currentTarget.classList;
const decrease = "decrease";
const increase = "increase";
// console.log(styles);
if (styles.contains(decrease)) {
count--;
} else if (styles.contains(increase)) {
count++;
} else {
count = ZERO;
}
if (count > ZERO) {
value.style.color = greenColor;
}
if (count < ZERO) {
value.style.color = redColor;
}
if (count === ZERO) {
value.style.color = blackColor;
}
value.textContent = count;
});
});