Build a Voting System - Build a Voting System

Tell us what’s happening:

Test 17 won’t pass at all. Just wondering what the issue is. Thanks.

Your code so far

let poll = new Map();

function addOption(option) {
    if (option && poll.has(option)) {
    return `Option "${option}" already exists.`
    } else if (option && !poll.has(option)) {
    poll.set(option, new Set())
    return `Option "${option}" added to the poll.`
  } else {
  return `Option cannot be empty.`
}}

function vote(option, voterId) {
  const bool = poll.get(option)
    if (bool && !bool.has(voterId)) {
      poll.set(option, poll.get(option).add(voterId))
      return `Voter ${voterId} voted for "${option}".`
    } else if (bool && bool.has(voterId)) {
      return `Voter ${voterId} has already voted for "${option}".`
    } else {
    return `Option "${option}" does not exist.`
    }
}

addOption("Malaysia");
addOption("Turkiye");
addOption("Egypt");
vote("Malaysia", "Ryan")
vote("Turkiye", "Bob")
vote("Turkiye", "Netenyahu")
vote("Turkiye", "James")
vote("Egypt", "Robbie")
vote("Egypt", "Gregor")

function displayResults() {
let result = []
let resultStr = ''
  poll.forEach((value, key) => {
    result.push(`${key}: ${value.size} votes`)
  })
  for (const char of result) {
    resultStr += char + "\n"
  }
  return "Poll Results:\n" + resultStr
  
}

console.log(displayResults())

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Voting System - Build a Voting System

This doesn’t work either, not sure what I’m doing wrong:

function displayResults(poll) {
let res = `Poll Results:`
for (const [key, values] of poll.entries()){
res += `\n${key}: ${values.size} votes`
} return res
}

The last line cannot end in a newline.

It doesn’t - the newline is before the string..?

When I remove the last newline from your original code it works.

The new function you posted gives an error TypeError: can't access property "entries", poll is undefined

It doesn’t show an error for me both in console and in the fcc editor. It returns the info correctly, that’s really strange. Any idea why? For me it’s fine, but it just won’t pass the test. I’d like to know why if it’s something weird/unique about js to avoid it in the future.

Please post your updated full code.

Although again… when I remove the last newline from the first code you posted orignially, it works. So you just need to do that.

removed by moderator

yeah thank you, now it works - I had poll as an argument in the function definition. For some reason it wasn’t giving me a typeError the first time, but when I re pasted it did indeed. Appreciate the help.

1 Like

Glad you got it! :+1: Please don’t post the solution code to the forum though.

I wish the problem here was more clear, people often get tripped up by this. The output pretty much appears the same and it’s not really clear from the example if there’s a trailing newline or not.