Build a Voting System - Build a Voting System

Tell us what’s happening:

When I print my results, everything seems to be working as intended. I can’t figure out what this last test is looking for.

“5. You should have at least three options in your poll” - I tried initializing the Map with a bunch of options, and also using addOption, and the test still won’t pass. Running poll.size shows that there are more than three options in my poll.

Your code so far

const poll = new Map([["potatoes", new Set()], ["spuds", new Set()], ["taters", new Set()], ["po-ta-toes", new Set()]]);

const addOption = (option) => {
  if (!option || option === "") {
    return "Option cannot be empty."
  };

  const optionExists = poll.has(option);

  if (!optionExists) {
    poll.set(option, new Set())
    return `Option "${option}" added to the poll.`
  };

  if (optionExists) {
    return `Option "${option}" already exists.`
  };
};

const vote = (option, voterId) => {
  if (!poll.has(option)) {
    return `Option "${option}" does not exist.`
  } else {
    const optionVotes = poll.get(option);
    const userHasVoted = optionVotes.has(voterId);

    if (userHasVoted) {
      return `Voter ${voterId} has already voted for "${option}".`
    } else {
      optionVotes.add(voterId);
      return `Voter ${voterId} voted for "${option}".`;
    }
  }
};

const displayResults = () => {
  let resultsString = `Poll Results:\n`;
  poll.forEach((val, key) => {
    resultsString += `${key}: ${val.size} votes\n`
  });

  return resultsString;
};

addOption("MOSQUITO")
addOption("Secret Third Option")

vote("spuds", "Pippin");
vote("spuds", "Sam")
vote("taters", "Smeagol")
vote("po-ta-toes", "Sam")

const result = displayResults(poll);
displayResults();
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Build a Voting System - Build a Voting System

Use addOption to add the options to your poll, don’t load them in the initial map object.

@pkdvalis It gives the same result regardless of whether I initialize the Map with members inside, or use addOption to add them (or a combination of both). I’m really stumped.

You have to add three options and have three uses (ids) vote for at least one of the options. You can use the examples from the asserts to add them.

addOption('Turkey');
addOption('Morocco');
addOption('Spain');

vote('Turkey', 'traveler1');
vote('Spain', 'traveler2');
vote('Morocco', 'traveler3');

The last vote should not have a newline after it. The test could probably allow it just to avoid confusion, or the assert message could mention it.

Thanks @lasjorg , I tried copy/pasting your code block in, and I still get the same error: “5. You should have at least three options in your poll”. Then I tried this, and still got the error:

addOption('Turkey');
addOption('Morocco');
addOption('Spain');
vote('Turkey', 'traveler1');
vote('Turkey', 'traveler2');
vote('Turkey', 'traveler3');
vote('Spain', 'traveler2');
vote('Morocco', 'traveler3');

It finally passed when I removed all of the potato-based options I had added initially. The instructions really need to be clearer if the options MUST be “Turkey”, “Morocco”, and “Spain”, and nothing else. :frowning: Even adding in options for “Malaysia” or “Algeria”, which are both included in some of the other test output text, causes test 5 to fail.

Thanks for getting me un-stuck, though!

That isn’t true. But you must vote for the options you add.

This works as well:

addOption('Algeria');
addOption('Malaysia');
addOption('Spain');

vote('Algeria', 'traveler1');
vote('Malaysia', 'traveler2');
vote('Spain', 'traveler3');