Wherefore art thou trouble

Tell us what’s happening:
I’m having trouble checking for the values when there are multiple property/value pairs being passed through as “source”. Any help with this would be appreciated.

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var val = [];
  var thing = Object.keys(source);
  collection.forEach(function (item){
    for (i=0; i<thing.length; i++){
      if (item.hasOwnProperty(thing[i]) && item[thing[i]] == source[thing[i]]){
        arr.push(item);
      }
    }
  });
  // 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:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/wherefore-art-thou

Thanks! This is my implementation of your suggestion and it works!

function whatIsInAName(collection, source) {
// What’s in a name?
var arr = [];
// Only change code below this line
var val;
var thing = Object.keys(source);
collection.forEach(function (item){
for (i=0; i<thing.length; i++){
if (item.hasOwnProperty(thing[i]) && item[thing[i]] == source[thing[i]]){
val = true;
}else {
val = false;
}
}
if (val == true){
arr.push(item);
}
});
// Only change code above this line
return arr;
}

whatIsInAName([{ “a”: 1, “b”: 2 }, { “a”: 1 }, { “a”: 1, “b”: 2, “c”: 2 }], { “a”: 1, “b”: 2 });

You know, that’s making me wonder if any of my other algorithm solutions are just coincidentally working as well. I updated my code and added your objects. Does that fully accomplish what you were recommending?

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var val;
  var thing = Object.keys(source);
  collection.forEach(function (item){
    for (i=0; i<thing.length; i++){
      if (item.hasOwnProperty(thing[i]) && item[thing[i]] == source[thing[i]]){
        val = true;
      }else {
        val = false;
        break; //the new part
      }
    }
   if (val == true){
        arr.push(item);
   } 
  });
  // Only change code above this line
  return arr;
}

whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { z: 1, b: 2 });

This new solution is more like what I was saying and accomplishes what the challenge should be testing for. Congrats on writing a more robust function!

You could actually initialize val as true before the for loop and then just look for the case where the source property does not exist in item OR source property value does not match the item’s property value. See my comments in the code below.

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

  var thing = Object.keys(source);
  collection.forEach(function (item){
    var val = true; // assumes current collection object (item) will be added
    for (i=0; i<thing.length; i++){
      if ( !item.hasOwnProperty(thing[i]) || item[thing[i]] !== source[thing[i]] ){
        val = false; // was not found or did not match
        break; // stop for loop because item should not be added to arr
      }
    }
   if (val == true){
        arr.push(item);
   } 
  });
  // Only change code above this line
  return arr;
}
1 Like