Tell us what’s happening:
When I print my results, everything seems to be working as intended. I can’t figure out what this last test is looking for.
“5. You should have at least three options in your poll” - I tried initializing the Map with a bunch of options, and also using addOption, and the test still won’t pass. Running poll.size
shows that there are more than three options in my poll.
Your code so far
const poll = new Map([["potatoes", new Set()], ["spuds", new Set()], ["taters", new Set()], ["po-ta-toes", new Set()]]);
const addOption = (option) => {
if (!option || option === "") {
return "Option cannot be empty."
};
const optionExists = poll.has(option);
if (!optionExists) {
poll.set(option, new Set())
return `Option "${option}" added to the poll.`
};
if (optionExists) {
return `Option "${option}" already exists.`
};
};
const vote = (option, voterId) => {
if (!poll.has(option)) {
return `Option "${option}" does not exist.`
} else {
const optionVotes = poll.get(option);
const userHasVoted = optionVotes.has(voterId);
if (userHasVoted) {
return `Voter ${voterId} has already voted for "${option}".`
} else {
optionVotes.add(voterId);
return `Voter ${voterId} voted for "${option}".`;
}
}
};
const displayResults = () => {
let resultsString = `Poll Results:\n`;
poll.forEach((val, key) => {
resultsString += `${key}: ${val.size} votes\n`
});
return resultsString;
};
addOption("MOSQUITO")
addOption("Secret Third Option")
vote("spuds", "Pippin");
vote("spuds", "Sam")
vote("taters", "Smeagol")
vote("po-ta-toes", "Sam")
const result = displayResults(poll);
displayResults();
console.log(result);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Voting System - Build a Voting System