Build a Voting System - Build a Voting System

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

Poll Results:/nTurkey: 2 votes/nMorocco: 1 votes

The console is showing this result. Does this look like the example?

thankyou!!!
silly mistake

am i doing something wrong with adding more than 3 options?

Why? Is your code still not passing?

It does pass, just interested why I’m unable to add more than 3 options without the test failing,
The test said

  1. You should have at least three options in your poll .

Because the test is written like that

Do you want to open an issue for that? The mismatch between hint and what is being tested is a bug

I have created an issue for this

thanks i was scratching my head a little at this