Intermediate Alrogithm -- Wherefore Art Thou -- my for loop is ruining my life at the moment

Intermediate Alrogithm -- Wherefore Art Thou -- my for loop is ruining my life at the moment
0

#1

Working on this algorithm now: https://www.freecodecamp.org/challenges/wherefore-art-thou

The code below is only pushing an object into the array a single time. For some reason, it won’t push a second object into it, even if the condition is true. I’m not sure what I’m missing, but I believe I just made my for loop incorrectly. I’m pretty open to being wrong. I have no idea what I’m doing in general.

 function whatIsInAName(collection, source) {
  var arr = [];
  var sourceProperty = Object.keys(source);



  for (i=0; i<collection.length; i++)
    {
      if (collection[i][sourceProperty] === source[sourceProperty]) {
        arr.push(collection[i]);
        return arr;
    
  }
  
}


}

#2

I might be wrong since I haven’t tested it but it looks like you are missing your declaration of ‘i’ within your for loop
so try

(var i = 0; …)

EDIT: Just checked and it seems that it doesn’t fix the code.

So anyway… You are returning your array way too early, if you look at the test cases, some of them have more than one object, this means that you have to return your array once the iteration is done (HINT, move return arr down by 2 bracers).

You also need to make sure that you are checking for all object keys, currently, you are checking for one.


#3

Thanks!! Perfect help!