Javascript algorithm scripting- Wherefore art thou

Confused on the solutions for the code… i found did not use the code template… any help will be much appreciated

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  for (var i = 0; i <source.length; i++) {
    if (collection.hasOwnProperty(source[i])) {
      for (var j = 0; j < collection.length; j++) {
        if ( collection[j] === source[i]) {
          arr.push(collection[j]);
          console.log(arr);
        }
      }
    }
  }
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/

Just want to point out, you should ask yourself what is the type of source. It may change your approach on accessing the data inside.

1 Like

as @zapcannon99 intimated, it looks like source is not an array so saying source[i] will not produce what you expect.

1 Like

ok thank you i will use object.keys only then as it seems like the easiest method to loop through user created object keys, at the beginning stage i feel i am concentrating more on getting an understanding instead of focusing on time and memory consumption