Build a voting system errors

hi, using a screen reader jaws for windows 2025 and using windows 11 2025. google chrome 144. now totally blind. doing the build a voting system. and steps 5,6 and 17 are falling over or failing. reset the lesson a few times and done a hard refresh a couple of times. so can any one help. is it my code, or the way fcc tests. so what is fcc tests looking for. can you explain, and then help me out to get it to pass. pasting the java script below for steps 5,6, and 17. and then the errors and the lab.

thank you.

marvin.

java script:

// Voting System JS - forum sample (steps 5,6,17 failing)

const poll = new Map();

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

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

function displayResults() {
let results = “Poll Results:”;
poll.forEach((voters, option) => {
results += \n${option}: ${voters.size};
});
return results;
}

// No prepopulation: FCC tests will add options and votes

errors:

  • Passed:1. You should have a poll variable initialized to a new Map object.

  • Passed:2. You should define a function addOption that accepts a parameter option.

  • Passed:3. You should define a function vote that accepts two parameters, option and voterId.

  • Passed:4. You should define a function displayResults to display the poll results.

  • Failed:5. You should have at least three options in your poll.

  • Failed:6. Your poll should have at least three votes.

  • Passed:7. You should ensure each voting option maps to a Set object.

  • Passed:8. addOption("Egypt") should return Option "Egypt" added to the poll.

  • Passed:9. Adding an empty option should return "Option cannot be empty."

  • Passed:10. When Turkey is already added, addOption("Turkey") should return Option "Turkey" already exists.

  • Passed:11. When Malaysia exists in the voting options, vote("Malaysia", "traveler1") should return Voter traveler1 voted for "Malaysia".

  • Passed:12. vote should update the Set of voters for an option.

  • Passed:13. When traveler1 tries to vote for Algeria again, vote("Algeria", "traveler1") should return Voter traveler1 has already voted for "Algeria".

  • Passed:14. Duplicate votes should not increase the size of the Set.

  • Passed:15. When Nigeria is not in the voting options, vote("Nigeria", "traveler2") should return Option "Nigeria" does not exist.

  • Passed:16. A unique option should be able to receive multiple votes.

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

  • project:

  • Skip to content

    Menu

    Profile

    1. Certified Full Stack Developer Curriculum

    2. Build a Voting System

    Instructions

    script.jsEditor

    Console

    Build a Voting System

    In this lab, you will build a voting system that uses Map to create a poll and Set to prevent duplicate voting.

    Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.

    User Stories:

    1. You should initialize a poll variable to a new Map object.

    2. You should have a function addOption that accepts a parameter option.

    3. In the addOption function:

      • If the option does not already exist in the poll, it should be added to the poll with an empty Set as its value to track voters. You should also return the message Option "<option>" added to the poll.

      • If the option already exists, it should return the message Option "<option>" already exists..

      • If you try to add an empty option, the function should return the message Option cannot be empty..

    4. You should have a function vote that accepts two parameters, option (the option to vote for) and voterId (a unique ID for the voter).

    5. In the vote function:

      • If the option does not exist in the poll, the function should return the message Option "<option>" does not exist..

      • If the option exists, the function should check if the voterId has already voted for this option.

      • If the voter has already voted, the function should return the message Voter <voterId> has already voted for "<option>".

      • If the voter has not voted, voterId should be added to the Set of voters for this option. The function should return the message Voter <voterId> voted for "<option>".

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

    7. Your poll should have at least three votes.

    8. You should have a function displayResults that returns the poll results in the following format:

    Poll Results:
    OptionA: N votes
    OptionB: N votes
    .
    .
    
    /*
    sample output
    
    Poll Results:
    Turkey: 2 votes
    Morocco: 1 votes
    */
    
    

    Run the Tests (Ctrl + Enter)

    Reset this lesson

    Get Help

    Tests

    • Waiting:1. You should have a poll variable initialized to a new Map object.

    • Waiting:2. You should define a function addOption that accepts a parameter option.

    • Waiting:3. You should define a function vote that accepts two parameters, option and voterId.

    • Waiting:4. You should define a function displayResults to display the poll results.

    • Waiting:5. You should have at least three options in your poll.

    • Waiting:6. Your poll should have at least three votes.

    • Waiting:7. You should ensure each voting option maps to a Set object.

    • Waiting:8. addOption("Egypt") should return Option "Egypt" added to the poll.

    • Waiting:9. Adding an empty option should return "Option cannot be empty."

    • Waiting:10. When Turkey is already added, addOption("Turkey") should return Option "Turkey" already exists.

    • Waiting:11. When Malaysia exists in the voting options, vote("Malaysia", "traveler1") should return Voter traveler1 voted for "Malaysia".

    • Waiting:12. vote should update the Set of voters for an option.

    • Waiting:13. When traveler1 tries to vote for Algeria again, vote("Algeria", "traveler1") should return Voter traveler1 has already voted for "Algeria".

    • Waiting:14. Duplicate votes should not increase the size of the Set.

    • Waiting:15. When Nigeria is not in the voting options, vote("Nigeria", "traveler2") should return Option "Nigeria" does not exist.

    • Waiting:16. A unique option should be able to receive multiple votes.

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

    1

    2

    3

    4

    5

    6

    /**
    * Your test output will go here
    */
    

can you explain which part of your code do you think satisfies the first failing test?

how are you adding the 3 options in your poll?

you have a comment in your code that say “No prepopulation: FCC tests will add options and votes”, don’t you think that’s against the user story and test that checks for the presence of three options?

Hey dude, a couple of things.

First make sure you do include all the words in the final return value.

At the moment your return is simply Country: number. There’s a word missing from your return that is included on the sample output.

Secondly, where you put the \n matters, but I think you have yours in the right spot :grin:.

Also ILM is right, you are failing user stories 5 and 6, which means you need to populate your dummy poll.