Please help needed here!
I have problem passing the following user stories of the “Building a Voting System”
When Malaysia exists in the voting options, vote(“Malaysia”, “traveler1”) should return Voter traveler1 voted for “Malaysia”.
Every other option I added passed except Malaysia; I don’t know how that is possible.
A unique option should be able to receive multiple votes.
Initially, I thought it was a single vote system preventing any voter from voting twice, but as I found out it was to multiple voting system it still hasn’t passed
displayResults() should return the results in the correct format. I don’t understand why this should fail as well
Please help needed here!
I have problem passing the following user stories of the “Building a Voting System” user story 11.) When Malaysia exists in the voting options, vote(“Malaysia”, “traveler1”) should return Voter traveler1 voted for “Malaysia”. User story 16.) A unique option should be able to receive multiple votes. User story 17.)displayResults() should return the results in the correct format.
Your code so far
const poll = new Map();
function addOption(option) {
// Check for null, undefined, or empty/whitespace
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.`;
}
console.log(addOption("Egypt"));
console.log(addOption("Turkey"));
console.log(addOption("Malaysia"));
console.log(addOption("Algeria"));
function vote(option, voterId) {
if (!poll.has(option)) return `Option "${option}" does not exist.`;
const voters = poll.get(option);
// Check if the voter has already voted for THIS option yet it failed no. 16
if (voters.has(voterId)) return `Voter ${voterId} has already voted for "${option}".`;
voters.add(voterId);
return `Voter ${voterId} voted for "${option}"`;
}
console.log(vote("Turkey", "Clarence"));
console.log(vote("Malaysia", "Steve"));
console.log(vote("Algeria", "Cleverly"));
console.log(vote("Algeria", "Josh"));
console.log(vote("Algeria", "Trinity"));
console.log(vote("Turkey", "Prince"));
console.log(vote("Egypt", "Cajetan"));
console.log(vote("Malaysia", "traveler1")); // This logs correctly but failed no. 11. I can't say what is wrong with the code since others passed
console.log(vote("Turkey", "Trinity"));
function displayResults() {
//This function logs correctly as the sample output, yet it failed no.17
let result = "Poll Results:\n";
for (const [option, voters] of poll.entries()) {
const count = voters.size;
const word = count === 1 ? "vote" : "votes";
result += `${option}: ${count} ${word}\n`;
}
return result;
}
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/141.0.0.0 Safari/537.36
I think I did earlier; that notwithstanding: I will itemize and explain my codes on the user stories that I failed.
11.) When Malaysia exists in the voting options, vote(“Malaysia”, “traveler1”) should return Voter traveler1 voted for “Malaysia”. I understand this not to be different from other options on the poll. Those other options passed except for Malaysia, and yet it logged accurately in the console. The addOption() added Malaysia as an option to the poll.
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.\`;
}
addOption("Egypt");
addOption("Turkey");
addOption("Malaysia");
addOption("Algeria");
And then the vote function that checks who votes for Malaysia for example is here:
User story 16.) A unique option should be able to receive multiple votes.
This right here is what I don’t know how to handle. Initially I understood it to mean a single vote per individual but it wasn’t. So honestly, I don’t know how to deal with this. The challenge here is with the vote function.
User story 17.) says displayResults() should return the results in the correct format.
I don’t know what I did wrong. The code:
function displayResults() {
let result = "Poll Results:\\n";
const sortedResult = Array.from(poll.entries()).sort((a,b) => b\[1\].size - a\[1\].size);
for(const \[option, voters\] of sortedResult) {
const count = voters.size === 1 ? "vote" : "votes" ;
result += \`${option}: ${voters.size} ${count}\\n\`;
}
return result;
}
console.log(displayResults());
I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add the backticks.
I appreciate your patience and readiness to help me. Please find below my own understanding about what I did and how I think it should work.
function addOption(option) {
// Check for null, undefined, or empty/whitespace
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.`;
}
// Adds Malaysia, amongst other options, to the voting poll as an option.
console.log(addOption("Egypt"));
console.log(addOption("Turkey"));
console.log(addOption("Malaysia"));
console.log(addOption("Algeria"));
// All of them logged in correctly to the console. Below is copied directly from the preview/console section of the IDE
Option "Egypt" added to the poll.
Option "Turkey" added to the poll.
Option "Malaysia" added to the poll.
Option "Algeria" added to the poll.
// Now to the vote function
function vote(option, voterId) {
if (!poll.has(option)) return `Option "${option}" does not exist.`;
const voters = poll.get(option);
// Check if the voter has already voted for the option
if (voters.has(voterId)) {
voters.add(voterId);
return `Voter ${voterId} has already voted for "${option}".`;
}
voters.add(voterId);
return `Voter ${voterId} voted for "${option}"`;
}
// Malaysia has two votes and it logged correctly to the console
console.log(vote("Turkey", "Clarence"));
console.log(vote("Malaysia", "Steve"));
console.log(vote("Algeria", "Cleverly"));
console.log(vote("Algeria", "Josh"));
console.log(vote("Algeria", "Trinity"));
console.log(vote("Turkey", "Prince"));
console.log(vote("Egypt", "Cajetan"));
console.log(vote("Malaysia", "traveler1"));
console.log(vote("Turkey", "Trinity"));
// Copied directly from the preview/console section of the IDE
Voter Clarence voted for "Turkey"
Voter Steve voted for "Malaysia"
Voter Cleverly voted for "Algeria"
Voter Josh voted for "Algeria"
Voter Trinity voted for "Algeria"
Voter Prince voted for "Turkey"
Voter Cajetan voted for "Egypt"
Voter traveler1 voted for "Malaysia" // just observed that others are colored orange except "traveler1"
Voter Trinity voted for "Turkey"
I am yet to decipher what is wrong with the displayResults function. The console says: 17. displayResults() should return the results in the correct format.
function displayResults() {
let result = "Poll Results:\n";
for(const [option, voters] of poll.entries()) {
const count = voters.size === 1 ? "vote" : "votes";
result += `${option}: ${voters.size} ${count}\n`;
}
return result;
}
console.log(displayResults());
/*
Poll Results:
Egypt: 1 vote ---> Option A
Turkey: 3 votes ---> Option B
Malaysia: 2 votes --> Option C
Algeria: 3 votes ---> Option D
*/