Help: Wherefore art thou - algorithm

Is my if Statement correct?
I am checking the property in source weather collection or not
if it is there, i push it into arr.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  source = Array.from(arguments).slice(1)
  for(let i=0; i<collection.length; i++){
    for(let j=0; j<source.length; j++){
      if(collection[i].hasOwnProperty(Object.keys(source[j])) ){
        arr.push(collection[i]);
      }
    }
  }
  // Only change code above this line
  return arr;
}

Yes but you didn’t check whether the values are same or nah (you need to check the values too to pass the test).

Also you might miss this clue.

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument).

Notice that the second argument is key value pairs therefore not an Array but an Object. So you don’t have to loop the source.

Here’s your code without the source loop.

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  for (let i = 0; i < collection.length; i++) {
    if (collection[i].hasOwnProperty(Object.keys(source))) {
      { arr.push(collection[i]); }
    }
  }
  // Only change code above this line
  return arr;
}

1 Like

Thank you @danzel-py
I am not getting any idea of checking value pairs

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.