Tell us what’s happening:
Question 17(displayResults) is formatted correctly but is not verifying that it’s correct
Your code so far
const poll = new Map()
function addOption(option){
if (poll.has(option)){
return `Option "${option}" already exists.`} else if (!option || "") {
return `Option cannot be empty.`
} else {
const voters = new Set();
poll.set(option, voters)
return `Option "${option}" added to the poll.`
}
}
console.log(addOption('e'))
console.log(addOption('e'))
console.log(addOption())
function vote(option, voterId){
const voters = poll.get(option)
if (!poll.has(option)){
return `Option "${option}" does not exist.`
} else if (voters.has(voterId)){
return `Voter ${voterId} has already voted for "${option}".`
} else {
voters.add(voterId)
return `Voter ${voterId} voted for "${option}".`
}
}
console.log(vote('e', '2'))
console.log(vote("e", '5'))
function displayResults(){
let resultString = `Poll Results:\n`
for (const [option, voters] of poll.entries()){resultString += `${option}: ${voters.size} votes\n` }
return resultString
}
addOption('3')
addOption('4')
vote('3', '4')
vote('4', '3')
console.log(displayResults())
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Voting System - Build a Voting System