Wherefore art thou - Help needed

Hi,

I’m currently working on the “Wherefore art thou” algorithm, but I’m not being able to make the last 2 tests green. Here’s my solution:

function whatIsInAName(collection, source) {
  var properties = Object.keys(source);

  var arr = collection.filter(function(item) {
    for(var i = 0; i < properties.length; i++) {
      if(item.hasOwnProperty(properties[i]) && item[properties[i]] == source[properties[i]]) {
        return true;
      } else {
        return false;
      }
    }
  });

  return arr;
}

Can anyone help me with this, please?

Thanks

It looks like your for loop never gets past i = 0 - on the first iteration you are either returning true or false without checking other properties elements.

Hint - you should return true only on the last iteration.

2 Likes

Thank you for the hint @jenovs.
Here’s how I fixed it:

function whatIsInAName(collection, source) {
  var properties = Object.keys(source);
  var result;
  
  var arr = collection.filter(function(item) {
    for(var i = 0; i < properties.length; i++) {
      if(item.hasOwnProperty(properties[i]) && item[properties[i]] == source[properties[i]]) {
        result = true;
      } else {
        result = false;
      }
    }
    
    return result;
  });

  return arr;
}
1 Like

thank you so much for this. I have been stuck on this one for five days, and banging my head against the wall while stuck. It turns out that I was missing the “result = true / result = false” statements. I don’t fully understand exactly why that bit of code made such a difference though… However I will figure that out before moving on to the next one.

Hi @TylerDevers ,
I also struggled a bit with this exercise. As @jenovs mentioned the “for loop never gets past i = 0”, this is because I was returning true/false, so the loop was not going through the other properties. The loop was ‘exiting’ the first time it found a return statement. Now the true/false result is being saved in a variable and only, in the end, we return the result, which means that the for loop will iterate through all the properties. Hope this helps :slight_smile:

1 Like

Okay, that statement made it click. I was troubleshooting the code by printing true and false to the console. Using the return statements, the loop returned true three times and printed nothing else. but when assigning true/false to the variable and then printing to the console it actually returned true, then false etc.

That now makes sense. With the return statement, the loop was only checking the first key:value pair, and then exiting the loop. I could see this happening but I did not know it was caused by the return statement! Though I feel like I should have. :slight_smile:

Thank you for helping me understand that.

I was doing this too (including beating my head against the keyboard). Glad I came across this. So having true/false assigned to the variable is what makes it tick. Can’t believe I didn’t figure that out on my own :blush:
Thanks to all.

Hey guys, sorry to wake this thread up from the dead. Could anyone explain to me please what’s the meaning of item[properties[i]] == source[properties[i]])?
Thanks!

Hey Guy

item[properties[i]] is the current value of “collection” being checked against “source” with the same key…
it is confusing because “item” , which you will find in the function, is the current iteration, of each object, in the “collection” array.

Hope that helps.

The use of ‘item’ was slightly confusing - but I think have it figured out now.
Thanks for your help!

Thanks for this digging up, I’d have a question about the solution here explained:
this solution creates a tie between the indexes of two different arrays. This means that if source does not have his own properties in the same order as collection’s objects has, the comparison goes wrong… doesn’t it?

Anyway, just done this challenge and for the first time I had to write on paper my thoughts…things are going to be thug, I guess :smiley:

After going back to this problem time and again, I finally managed to solve it after stumbling upon this thread today.
I guess the “helpful links” section made me overthink it a bit.

This thread helped me solve the challenge today. Thanks!


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var key = Object.keys(source);
  var value = source[key];
  var i;
  var result;
  
  while(i< Object.keys(collection).length){
    if(collection[i].hasOwnProperty(key) && collection[i].key==value){
      result = collection[i];
    }else{
      i++;
    }
  }
 return result;
 
  // Only change code above this line
  
}

i really feel bad bc last two algorithm challenges are too hard for me. it is my 3rd week with javascript and i am totally lost.

does anybody know why my code doesn’t work?

@flavia.rodriguesf Thank you so much for posting! I struggled mightily with this one and your code helped me work through my own thoughts. I had the same idea to iterate through the key-values pairs within a filter but I couldn’t get it to work out and the other posts weren’t doing anything but confusing me. You rock!

Quick update that this no longer passes the additional tests FCC added but I think I worked it out. Here’s an addition that doesn’t pass:

[{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3} should return []

but it returns

{"a": 1, "b": 2, "c": 3}

I’m guessing that because the first key-value pair (“a: 1”) evaluates as true then, although the second pair (“b” : 9999") changes the result to false, it gets changed right back to true on the third pair. That scum! I passed the code by adding a return to the false statement so that it will stop looping through it key-value pairs if ANY of them is false:

function whatIsInAName(collection, source) {

  let srcKeys = Object.keys(source);
  let value;

  let filtered = collection.filter((x) => {
    for(let y = 0; y < srcKeys.length; y++){
      if(x.hasOwnProperty(srcKeys[y]) && x[srcKeys[y]] == source[srcKeys[y]]){
        value = true;
      } else {
        return false;  // if false stop looping right here and move on
      }
    }
    return value;
  });
     console.log(filtered);
     return filtered;
}

Cheers, FCC family!

1 Like
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  collection.forEach(function(ele) {
    for(var prop in source) {
      if(source[prop] !== ele[prop]) {
         return arr;
      }
    }
    arr.push(ele);
  })
  
  // Only change code above this line
  return arr;
}
1 Like