Questions of wrong method, notation in Wherefore art thou

Tell us what’s happening:
I am using a for loop on this one and I’m only passing the first two tests. I’m not sure where I need to adjust the code. I’d tried a 2d loop, but that didn’t seem to work and also I wasn’t sure why it was necessary as I only need to find one instance of similarity with the property “last” and I’ve done that.

But what am I overlooking? What have assumed wrongly?
I wonder if I should be using .push()?
Or, if the notation of source is incorrect?
Also, I initially assumed this was JSON but saw that the keys were not in quotes. Could this what’s wrong? Should I stringify?

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 < collection.length; i++){
   if(collection[i].last == source.last){
  arr.push(collection[i]);
  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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

Your issue is a logic problem, rather than a syntax one. You are always checking whether the two items have the same last property. Read the instructions again carefully and look at the the values used in the test. last was the property that was being checked in the example, but that’s only an example.

1 Like

Dang it. @ArielLeslie totally beat me to the punch.

Yeah, in the line I cite above, you’re looking for the property last on both source and collection[i], but that won’t always be the property name. source is providing you with both the property name, and the desired value.

The instructions for this one do actually make pretty good sense, definitely re-read them!

So what I have tried is using the parameter source (without property) as triple equaled to <code?>collection[i] and pushing collection[i] OR source into the empty arr. Neither of these approaches worked. I also tried to concat() onto arr. This isn’t working either.

So, it’s logic I’m struggling with. I think the above methods are dealing with logic, but perhaps I’ve now switched to syntax being my problem? I’m not sure. Help please.

So source is being passed to you in a format like so:

{ name : value }

How can you get the name of the property? How about using Object.keys()? This returns an array of property names for the given object. In our case, we want only one, so we can get the key at index position zero:

let name = Object.keys(source)[0]

Having done that, you can then access the value as

let theValue = source[name];

Now, you’re being told to return any member of collection with the property name and value that matches source. Using what you just got, can you see a way to do that?

1 Like

I’m still not passing a majority of the tests and my intuition is that I’m not doing the syntax of my if statement properties, at least in terms of how I deal with properties from objects. I’m not sure what to do about that YET, but I’d like to know if my intuition is false or not.


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Obtain values for name and value properties for second argument
let name = Object.keys(source)[0]
let value = source[name];
//filter through collection array checking for identical key:value pairs
//if there is match, then push that onto empty array and return this array
collection.filter((val)=>{
for(var val = 0;val < collection; val++){
  if(collection[val] === name, value){
    arr.push(this.collection[val])
  }
 
}
});
  
  // Only change code above this line
  return arr;
}

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

What are you referencing with this?

Oh, that was a previously failed attempt I forgot to delete. I thought if I used this to identify the exact item I wanted, then the result of the interaction between val and collection array could be specific enough for the computer to understand the item I wanted.

This didn’t work and I forgot to remove it. That said, my problem still remains. What should I be looking at that I’m not?

let name = Object.keys(source)[0]

With the above code you are just using one property of the source object. What if the source object passes more than one property? Look at this test whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }). The source object has two properties apple & bat.

What are you trying to do with collction.filter in your code? Check MDN to better understand the filter method - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

1 Like