Challenge 4 Megathread

Welcome to challenge 4! Use this thread for any and all questions relating to challenge 4.

Having difficulty with this challenge? Check out the videos below from members of the community for great tutorials.

1 Like

Hi,
I’m trying day 04 but I have failed 1 test.
This is failed test:

You have 1 Failing Test.

Failed: The registerToVote function should remove an element from the array.

Reason: Your code ran but did not produce the correct result.

Hint: Make sure you’re not returning the original array.

Failing Test Code:

      assert.deepStrictEqual(registerToVote('Bobby', ['Jimmy', 'Suzie', 'Bobby']), ['Jimmy', 'Suzie']);
      assert.deepStrictEqual(registerToVote('Floyd', ['Tomasa', 'Tennille', 'Tayna', 'Eufemia', 'Floyd']), ['Tomasa', 'Tennille', 'Tayna', 'Eufemia']);
      assert.deepStrictEqual(registerToVote('Bradley', ['Jake', 'Alanna', 'Bradley', 'Stephanie']), ['Jake', 'Alanna', 'Stephanie']);
      

Expected: Jimmy,Suzie

Actual:
This is my code:
const registerToVote = (name, unregisteredVoters) => {
// Code here!

let mailingList = [];

for (let v in mailingList) {
  
  if (name !== " ") {
    
    mailingList.push(name);
    
    
  }
  
  else {
    
    mailingList.remove(name);
    
    return unregisteredVoters;
  }

}

return mailingList;

// Remember to return a value!
}

I don’t understand why with remove() doesn’t work?

it seems you are looping an empty array – mailingList;
Loop the unregisteredVoters cos thats what you populated,
for (let v in unregisteredVoters)
then your logic should compare with what you are populating.
dont hard code.
your return statement should be outside the for loop statement in order to return the array.

you can do this. lets go

But in the hint they suggest to declare an empty Array called mailingList.

1 Like

const registerToVote = (name, unregisteredVoters) => {
// Code here!
let mailingList = [];
for(let i in unregisteredVoters){
if(unregisteredVoters[i] === name){
unregisteredVoters.splice(i,1);
mailingList.push(unregisteredVoters);

}

}
return mailingList;

// Remember to return a value!
}

I tested on a browser console and it is working, but for the challenge.
Any Idea why ?
It is not fear if there is more solutions
Thanks!

splice takes more than one argument. check the splice statement

Yes, I noticed it and I changed it; however, I tried and it does not work. I tested in my browser console and it work fine after I add the second argument to the splice. I dont know why in the challenge does not work.


Here, it is passing all the testes after I fixed the missing argument in the splice function.

Solution spoiler alert!

Attempt the challenge before watching this video: https://youtu.be/SfIPAAS290E

Thanks for all the comments on yesterday’s video. Good luck!

I have some difficult to resolve this ,because I don’t know what I’m doing wrong yet.

const registerToVote = (name, unregisteredVoters) => {
  // Code here!
    
    let mailingList = [];
   
    for (let v in unregistredVoters) {
      
      if (unregistredVoters[v] !== name) {
        
        unregistredVoters.remove(name);
       
      }
      
      else {
        
         mailingList.push(name);
   
      }
 
    } 
  
  return unregistredVoters ;
  
  // Remember to return a value!
}
1 Like

That is not fear my solution was working too.
Question:
Honestly answer pls ,
was my solution a valid answer to the challenge too?
if my answer was not valid let my know why .
Thanks!

There is someone who explains the logic behind this:

 if (name !== unregisteredVoters[v]){
        
         
        mailingList.push(unregisteredVoters[v]);
        
      }

I think I didn’t get it right

Besides using if/else or looping, we can use indexOf() and array.splice() to do this challenge with one code line.

When you are pushing unregisteredVoters to the mailingList array you are creating an array inside of an array,

mailingList.push(unregisteredVoters)

instead of pushing to the mailingList you can actually return the unregisteredVoters array directly. You have already removed the name from that array with your splice.

SPOILER ALERT For those who want to try the filter function, I used this line for the challenge : return unregisteredVoters.filter(function(unregisteredVoter){return unregisteredVoter != name});

2 Likes

You spelled unregistredVoters incorrectly, so that will have an impact. Not sure about the rest of the code, still working through the solution myself.

Used the same Line :+1:

1 Like

I saw…ops :sweat_smile:
In fact I corrected it in my code, I have to be careful to write next time.