Build a Voting System - Build a Voting System

Tell us what’s happening:

Can’t figure out why these two keep coming up. “Option cannot be empty” comes up in my results. I get the 2 Votes and 1 vote.
// running tests
9. Adding an empty option should return “Option cannot be empty.”
17. displayResults() should return the results in the correct format.

Your code so far

const poll = new Map();

function addOption(option) {
  if(option.length == 0) return `option cannot be empty.`;

  if(!poll.has(option)){
    poll.set(option, new Set());
    return `Option "${option}" added to the poll.`;
  } else {
    return `Option "${option}" already exists.`;
  } 
}
function 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}".`;
    }
  }
}

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

  let lastNewLineIndex = resultsStr.lastIndexOf('\n');
  return resultsStr.substring(0, lastNewLineIndex) + resultsStr.substring(lastNewLineIndex + 1);
} 

console.log(addOption("Egypt"));
console.log(addOption("Ethiopia"));
console.log(addOption("Cameroon"));
console.log(addOption(""));
console.log(vote("Ethiopia", "traveler1"));
console.log(vote("Ethiopia", "travler2"));
console.log(vote("Algeria", "traveler1"));
poll.set("Algeria", new Set());
console.log(vote("Algeria", "traveler1"));
console.log(vote("Algeria", "traveler1"));
console.log(vote("Nigeria", "traveler3"));

console.log("\nResults:")
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/138.0.0.0 Safari/537.36 Edg/138.0.0.0

Challenge Information:

Build a Voting System - Build a Voting System
https://www.freecodecamp.org/learn/full-stack-developer/lab-voting-system/build-a-voting-system

looks like you forgot to capitalize the word Option

Edit: can you find the other mistake? (it’s one letter that is missing)

Capitalization must exactly match

  1. displayResults() should return the results in the correct format.

Yes, that is the error message. Which lines of code is that related to? Which one might have a typo?

this error is trying to tell you that your output is not matching the requirements given to you.
As I mentioned earlier, you have a letter missing in your output. Try to find it.

1 Like

function addOption(option) {
if(Option.length == 0) return option cannot be empty.;

I fixed that error and it 's still giving me the same error.

what did you fix? How did you fix it?
Which error is still showing?
Have you found the missing letter in your output?

aren’t you getting an error that something is undefined?

do not make random changes, understand what you are being suggested to change

No! I do not get an error that something is undefined. // running tests
17. displayResults() should return the results in the correct format.
// tests completed
// console output
Option “Egypt” added to the poll.
Option “Cameroon” added to the poll.
Option “Ethiopia” added to the poll.
Option cannot be empty.
Option “Ethiopia” already exists.
Voter traveler1 voted for “Cameroon”.
Voter traveler2 voted for “Cameroon”.
Option “Algeria” does not exist.
Voter traveler1 voted for “Algeria”.
Voter traveler1 has already voted for “Algeria”.
Option “Nigeria” does not exist.

Results:
Poll Result:
Egypt: 0 votes
Cameroon: 2 votes
Ethiopia: 0 votes
Algeria: 1 votes

Focus carefully on User Story #8. Is your output formatted like that?

1 Like

Thank you. I found it!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.