-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-a-voting-system.js
More file actions
46 lines (35 loc) · 953 Bytes
/
Copy pathbuild-a-voting-system.js
File metadata and controls
46 lines (35 loc) · 953 Bytes
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
const poll = new Map();
function addOption(option) {
if (!option || option.trim() === "") {
return "Option cannot be empty.";
}
if (poll.has(option)) {
return `Option "${option}" already exists.`;
}
poll.set(option, new Set());
return `Option "${option}" added to the poll.`;
}
function vote(option, voterId) {
if (!poll.has(option)) {
return `Option "${option}" does not exist.`;
}
const voters = poll.get(option);
if (voters.has(voterId)) {
return `Voter ${voterId} has already voted for "${option}".`;
}
voters.add(voterId);
return `Voter ${voterId} voted for "${option}".`;
}
function displayResults() {
let result = "Poll Results:";
poll.forEach((voters, option) => {
result += `\n${option}: ${voters.size} votes`;
});
return result;
}
addOption("Turkey");
addOption("Morocco");
addOption("Spain");
vote("Turkey", "voter1");
vote("Turkey", "voter2");
vote("Morocco", "voter1");