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
@pkdvalis It gives the same result regardless of whether I initialize the Map with members inside, or use addOption to add them (or a combination of both). I’m really stumped.
Thanks @lasjorg , I tried copy/pasting your code block in, and I still get the same error: “5. You should have at least three options in your poll”. Then I tried this, and still got the error:
It finally passed when I removed all of the potato-based options I had added initially. The instructions really need to be clearer if the options MUST be “Turkey”, “Morocco”, and “Spain”, and nothing else. Even adding in options for “Malaysia” or “Algeria”, which are both included in some of the other test output text, causes test 5 to fail.