Tell us what’s happening:
Hi
The last test is failing, I’ve read the other posts about removing the trailing new line character which I’ve have done with slice, trimEnd and creating an array and joining with a new line character, none of them passed the test, so unsure what to attempt next.
Also the tests say to add at least 3 options, if i add 3 that test passes but if i add 4 or more options as I did initially the at least 3 options test fails.
thanks
Your code so far
const poll = new Map();
const addOption = (option) => {
if(!option.length) return `Option cannot be empty.`;
const isOptionInPoll = poll.has(option);
if(!isOptionInPoll) {
poll.set(option, new Set());
return `Option "${option}" added to the poll.`;
}
else return `Option "${option}" already exists.`;
}
// addOption("dave");
// addOption("john");
addOption("chas");
addOption("barry");
addOption("paul");
const vote = (option, voterId) => {
const isOptionInPoll = poll.has(option);
if (!isOptionInPoll) return `Option "${option}" does not exist.`;
const optionVotes = poll.get(option);
const voter = optionVotes.has(voterId)
if (voter) return `Voter ${voterId} has already voted for "${option}".`
else {
optionVotes.add(voterId);
return `Voter ${voterId} voted for "${option}".`
}
}
vote("barry", 1);
vote("barry", 2);
vote("paul", 3);
vote("paul", 4);
vote("paul", 5);
vote("paul", 6);
const displayResults = () => {
let strArr = ['Poll Results:']
poll.forEach((value, key) => {
strArr.push(`${key}: ${value.size} votes`)
})
console.log(strArr.join("/n"))
return strArr.join("/n")
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build a Voting System - Build a Voting System