Tell us what’s happening:
Tests 17 and 18 are failing.
I have entered the amount of votes that test 17 says but the displayResults function isn’t printing them correctly and I am not sure where my logic is wrong.
Also I looked in my browser’s console and the format of the return value seems to be correct yet the test for it isn’t passing.
Your code so far
const poll = new Map();
poll.set("Turkey", new Set());
poll.set("Morocco", new Set());
poll.set("Spain", new Set());
function addOption(option) {
if (option === "") {
return "Option cannot be empty.";
}
for (const item of poll.keys()) {
if (item === option) {
return `Option "${option}" already exists.`;
}
}
poll.set(option, new Set());
return `Option "${option}" added to the poll.`;
}
function vote(option, voterId) {
for (const item of poll.keys()) {
if (poll.get(item).has(voterId)) {
return `Voter ${voterId} has already voted for "${option}".`
} else {
poll.get(item).add(voterId);
return `Voter ${voterId} voted for "${option}".`
}
}
return `Option "${option}" does not exist.`
}
function displayResults() {
let output = "Poll Results:";
poll.forEach((value, key) => {
console.log(value)
output+= "\n";
output+= `${key}: ${value.size} votes`
})
return output;
}
vote("Turkey", "1");
vote("Turkey", "2");
vote("Morocco", "3");
console.log(displayResults())
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:156.0) Gecko/20100101 Firefox/156.0
Challenge Information:
Build a Voting System - Build a Voting System