Build a Voting System - Build a Voting System - Tests 17 and 18

Tell us what’s happening:

Tests 17 and 18 are failing.

I have entered the amount of votes that test 17 says but the displayResults function isn’t printing them correctly and I am not sure where my logic is wrong.

Also I looked in my browser’s console and the format of the return value seems to be correct yet the test for it isn’t passing.

Your code so far

const poll = new Map();
poll.set("Turkey", new Set());
poll.set("Morocco", new Set());
poll.set("Spain", new Set());

function addOption(option) {
  if (option === "") {
    return "Option cannot be empty.";
  }
  for (const item of poll.keys()) {
    if (item === option) {
      return `Option "${option}" already exists.`;
    }
  }
  poll.set(option, new Set());
  return `Option "${option}" added to the poll.`;
}

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

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

vote("Turkey", "1");
vote("Turkey", "2");
vote("Morocco", "3");
console.log(displayResults())

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:156.0) Gecko/20100101 Firefox/156.0

Challenge Information:

Build a Voting System - Build a Voting System

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-voting-system/673b567e3ba535dda140d278.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @codingwookie,

Use your addOption function instead please.

Log your function calls so you can see what they return in the console.

What do you see in the console when you test like this:

console.log(addOption("Turkey"));
console.log(addOption("Morocco"));
console.log(addOption("Spain"));
console.log(vote("Turkey",1));
console.log(vote("Turkey",2));
console.log(vote("Morocco",1));
console.log(displayResults());

Happy coding

Hi,

I got the below after changing to use the addOption function and logging the calls.

Option “Turkey” added to the poll.
Option “Morocco” added to the poll.
Option “Spain” added to the poll.
Voter 1 voted for “Turkey”.
Voter 2 voted for “Turkey”.
Voter 1 has already voted for “Morocco”.
Poll Results:
Turkey: 2 votes
Morocco: 0 votes
Spain: 0 votes

Test 6 is now failing too.

And here is where you need to look for the bug in your code.