-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
92 lines (78 loc) · 2.77 KB
/
script.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const listers = document.getElementById('liters');
const percentage = document.getElementById('percentage');
const remained = document.getElementById('remained');
const minus = document.getElementById('minus');
const plus = document.getElementById('plus');
const smallCups = document.querySelectorAll('.cup-small');
const cups = document.getElementById("cups");
//converting NodeList from querySelectorAll() to an array
let smallCupsArr = Array.from(smallCups);
let goal = 2;
minus.addEventListener("click", () => updateGoal("-"));
plus.addEventListener("click", () => updateGoal("+"));
function updateGoal(sign) {
//take plus or minus string sign
if (sign == "+" && goal < 3.75) {
goal += 0.25;
addCup();
}
else if (sign == "-" && goal > 2){
goal -= 0.25;
deleteCup();
}
document.getElementById("goal").innerText = goal;
listers.innerText = `${goal}L`;
updateBigCup();
console.log(smallCupsArr);
}
const addCup = () => {
const newCup = document.createElement('div');
newCup.classList.add("cup", "cup-small");
const newContent = document.createTextNode("250 ml");
newCup.appendChild(newContent);
const INDEX = smallCupsArr.length;
newCup.addEventListener('click', () => highlightCups(INDEX))
cups.appendChild(newCup);
smallCupsArr.push(newCup);
}
const deleteCup = () => {
cups.removeChild(cups.lastChild);
smallCupsArr.pop();
}
updateBigCup()
smallCupsArr.forEach((cup, idx) => {
cup.addEventListener('click', () => highlightCups(idx))
})
function highlightCups(idx) {
if (idx === smallCupsArr.length - 1 && smallCupsArr[idx].classList.contains("full")) idx--;
else if(smallCupsArr[idx].classList.contains('full') && !smallCupsArr[idx].nextElementSibling.classList.contains('full')) {
idx--
}
smallCupsArr.forEach((cup, idx2) => {
if(idx2 <= idx) {
cup.classList.add('full')
} else {
cup.classList.remove('full')
}
})
updateBigCup()
}
function updateBigCup() {
const fullCups = document.querySelectorAll('.cup-small.full').length;
const totalCups = smallCupsArr.length;
if(fullCups === 0) {
percentage.style.visibility = 'hidden'
percentage.style.height = 0
} else {
percentage.style.visibility = 'visible'
percentage.style.height = `${fullCups / totalCups * 330}px`
percentage.innerText = `${Math.round(fullCups / totalCups * 100)}%`
}
if(fullCups === totalCups) {
remained.style.visibility = 'hidden'
remained.style.height = 0
} else {
remained.style.visibility = 'visible'
listers.innerText = `${goal - (250 * fullCups / 1000)}L`
}
}