[SOLVED] My "Wherefore art thou" code wont work inside nested loop but works outside loops?

[SOLVED] My "Wherefore art thou" code wont work inside nested loop but works outside loops?
0

#1

when I take everything inside the nested loop and put it outside and changed the indices to actual values, then it will return something. When it’s inside the loops, it doesn’t do anything.

This outputs [];

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  
  var sourcekeys = Object.keys(source);

  for (var i = 0; i < sourcekeys; i++){
    for (var j = 0; j < collection.length; j++){
      
      // Checks to see if the object has the property and if their values are the same
      if (collection[j].hasOwnProperty(sourcekeys[i]) && collection[j][sourcekeys[i]] === source[sourcekeys[i]]){
      
        arr.push(collection[j]);
        
      }
    }
  }

  
  // Only change code above this line
  return arr;
}

This actually returns something:

if (collection[0].hasOwnProperty(sourcekeys[0]) && collection[0][sourcekeys[0]] === source[sourcekeys[0]]){
      
        arr.push(collection[0]);
        
      }
  return arr;

#2

In the following for loop, you probably intended to use sourcekeys.length. Fixing this will not completely solve the challenge, but you will at least make it into the inner for loop.

for (var i = 0; i < sourcekeys; i++){

If you are still stuck, ask again for help and someone will give you some advice.


#3

I worked on it for two hours and I didn’t catch that! :joy: thanks! I figured out the rest, too :slight_smile: