What is the right way to solve this and pass all tests

this is the hardest challenge I faced after the record collection challenge.

I have tried a lot to reach this solution and it doesn’t pass all tests I do not know why cause I thought about what happens behind the scenes.

so the first thing I did is loop over the collection array which is an array of objects so, in every iteration, it should give me a different object using for of, inside the loop I chose to loop over the properties of the source object using for in to compare it with the properties of the objects from the array collection

I do not know if I thought it the right way but here is what I imagined inside the inner loop in every iteration it gets a different property from the source object and after that, I compare it with the property of the object of the array collection and I reached the property of the object using the bracket notation and put in it the variable that holds different a property every time to compare if it is identical

that is what I have reached and my solution worked for the first and second test but it did not work for the rest even the last test failed which is to return an empty array I don’t know why it failed but I hope that I could know the answer


function whatIsInAName(collection, source) {
const arr = [];

// now let us loop over the collection array 

for (let obj of collection) {

  // create anothe loop and check if the object contains the property in the source 
  for (let prop in source) {
    if(obj[prop] == source[prop]){
      arr.push(obj)
    }
  }
}

return arr;
}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })
  **Your browser information:**

Challenge: Wherefore art thou

Link to the challenge:

I think the problem might be here? You are pushing the obj immediately if it matches, but the problem requires you to match ALL source properties, correct?

1 Like

but the for-in loops over all the properties fo the source object so it should compare all of them

But it pushes as soon as the test is true. So, if it finds one prop that matches the source, it pushes to the array. Then, if later it ends up missing a second prop, it’s too late–the obj has already been pushed into the arr.

1 Like

why would it end missing a second prop if it already pushed the whole object

Which test cases is it saying you are passing and which are you not passing?

Are you failing because you are pushing too many values or too few?

1 Like

Look closely at the tests that are failing. What do they have in common? Specifically, how many properties does the source object have? Does your code currently take this into account?

as it seems my code is not dynamic to take this into account so there should be a way that I could test all the properties of the source object and after that I push it to the arr can you tell how can I make a certain condition like this

You are correct, this is what your current code is missing. There are a variety of ways to do this. One thing I might suggest is that you take a slightly different approach to iterating through the collection array. Basically, this challenge is asking you to select only certain items from the array. Another way to phrase this is that it is asking you to filter the array (remove the items that don’t meet the requirements). Perhaps JS provides an array method that will help you filter these items?

1 Like

I find that it’s helpful for me to put a lot of extra console.log calls in my code. I can use that to check in on various aspects of the program and make sure it’s executing correctly. I didn’t see them in your screenshot, so it looks like you don’t know exactly what’s going wrong with the failed tests. If you add enough extra info in, it can help diagnose the problem–you start to wonder, “Why is my code giving me THESE results? I was expecting THAT,” and you can trace through to see where the mistake lies.

1 Like

Side note - its best to provide the actual code instead of a picture. The actual code is easier to read, manipulate, comment, debug, etc.

1 Like

I have found this solution but I do not know if it is the right solution cause what I think it only compares the values of the properties, not the keys but it worked

here is the solution

function whatIsInAName(collection, source) {
  const arr = [];
  // Only change code below this line
  for (let obj of collection) {

    let matching = true

    for (let prop in source) {
      if(obj[prop] !== source[prop])
      matching = false;
    }

    if(matching){
      arr.push(obj)
    }
  }

  // Only change code above this line
  return arr;
}

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

I will as long as it is going to make it easier for me to detect things

The first thing you should add in most of these tasks is a console.log() for the return value - either before the return or for the function call at the bottom. Sometimes adding some more for inbetween-values.

Because you have the test cases and the expected results on the left for those you fail BUT you have no idea what’s actually getting returned.

Logging values is a vital part of debugging code :wink:

2 Likes

thanks I will do this to improve my ability in debugging

The reason that code works is because it checks EVERY property of the source and if ANY of them are not included in the target, then matching = false and the item is rejected. Your originally posted code would return the target when a single matching property was found.

I’m not sure if you had a chance to implement the extra debugging yet, but if so you probably would have seen that you were pulling too many values in your return.

1 Like

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