-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (44 loc) · 1.07 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
var buttons = document.querySelectorAll(".choice button"),
tally = {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
total: 0
};
function vote(choice) {
tally[choice]++;
tally["total"]++;
console.log(tally);
Object.keys(tally).forEach(poll => {
if(poll !== "total") {
document.getElementById(`choice-${poll}`).innerText = tally[poll];
}
})
}
function barPercentage(node, tally) {
var choice = node.dataset.choice;
if (tally[choice])
return tally[choice]/tally["total"] * 100;
return 0;
}
function renderBars() {
var bars = document.getElementsByClassName("bar");
for (var i = 0; i < bars.length; i++) {
var percentage = barPercentage(bars[i], tally);
console.log(percentage)
bars[i].style.height = percentage.toString() + "%";
}
}
function setup() {
// Set up event listeners
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function(e) {
vote(e.target.dataset["choice"]);
renderBars();
});
}
renderBars();
}
setup();