Build a Voting System - Build a Voting System

Tell us what’s happening:

Have been trying for a while and don’t know why the display is not working, please help me to figure out what am I missing to match the format.

Failed:17. displayResults() should return the results in the correct format.

Your code so far

const poll = new Map();

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

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

addOption("OptionA");
addOption("OptionB");
addOption("OptionC");
vote("OptionA", 1);
vote("OptionB", 2);
vote("OptionC", 3);

function displayResults(){
  let s = "Poll Results:\n";
  poll.forEach((value,key) => s += `${key}: ${value.size} votes\n`);
  return s;
}

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/134.0.0.0 Safari/537.36 Edg/134.0.0.0

Challenge Information:

Build a Voting System - Build a Voting System

Hi @gustavohome09

When I debug your code, it looks like you are adding an extra line after the last result.

The tests are not expecting the extra line.
Compare the expected output with your output.

I added the x to show the extra line at the end.

Happy coding

1 Like

That’s it, thank you!

1 Like