-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (43 loc) · 1.68 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
const options = [
{id:"option1", text:"Hyderabadi Chicken Dum Biryani", votes:0},
{id:"option2", text:"Pakhala Bhata", votes:0},
{id:"option3", text:"Double Cheese Margherita Pizza", votes:0},
{id:"option4", text:"Monte Cristo Sandwich", votes:0},
];
function submitVote(){
const selectedOption = document.querySelector('input[name="poll"]:checked');
if(!selectedOption){
alert("Please select an Option.");
return;
}
const optionId = selectedOption.value;
const selectedOptionObj = options.find((option)=> option.id === optionId);
//console.log(selectedOptionObj);
if(selectedOptionObj){
selectedOptionObj.votes++;
console.log(selectedOptionObj);
displayResult();
}
}
function displayResult(){
const result = document.getElementById('result');
result.innerHTML = "";
options.forEach((option)=>{
const percentage = ((option.votes/ getTotalVotes()) * 100).toFixed(2) || 0;
const barwidth = percentage > 0 ? percentage + "%" : "0%";
const optionResult = document.createElement("div");
optionResult.className = "option-result";
optionResult.innerHTML = `
<span class = "option-text">${option.text}</span>
<div class = "bar-container">
<div class = "bar" style="width: ${barwidth};"></div>
</div>
<span class = "percentage">${percentage}%</span>
`;
result.appendChild(optionResult);
});
}
function getTotalVotes(){
return options.reduce((total, option)=> total + option.votes, 0);
}
displayResult();