-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhat-fruit.js
50 lines (41 loc) · 1.55 KB
/
what-fruit.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
function calculateFruit() {
let watermelonCount = 0;
let durianCount = 0;
let bananaCount = 0;
let orangesCount = 0;
let pineappleCount = 0;
let grapesCount = 0;
let form = document.getElementById('fruitForm');
let selected = form.querySelectorAll('input[type="radio"]:checked');
for (let i = 0; i < selected.length; i++) {
if (selected[i].value === "Watermelon") {
watermelonCount++;
} else if (selected[i].value === "Durian") {
durianCount++;
} else if (selected[i].value === "Banana") {
bananaCount++;
} else if (selected[i].value === "Oranges") {
orangesCount++;
} else if (selected[i].value === "Pineapple") {
pineappleCount++;
} else if (selected[i].value === "Grapes") {
grapesCount++;
}
}
let winnerCount = Math.max(watermelonCount, durianCount, bananaCount, orangesCount, pineappleCount, grapesCount);
let winnerFruit = "";
if (winnerCount === watermelonCount) {
winnerFruit = "Watermelon";
} else if (winnerCount === durianCount) {
winnerFruit = "Durian";
} else if (winnerCount === bananaCount) {
winnerFruit = "Banana";
} else if (winnerCount === orangesCount) {
winnerFruit = "Oranges";
} else if (winnerCount === pineappleCount) {
winnerFruit = "Pineapple";
} else if (winnerCount === grapesCount) {
winnerFruit = "Grapes";
}
document.getElementById("result").textContent = "You are a " + winnerFruit + "!";
}