Wherefor Art Thou Help?

I’m stuck. I spent a while, with help from a friend and Mozilla Developer Network and got this function I wrote to pass three out of six tests. I’m passing tests where the “source” argument is an object with a single key value pair. But when the “source” argument in the function has two or three key value pairs, I get an empty array as my result. I think the problem is in the first part of my if statement, but I’m just completely out of ideas on how to fix it. I’ve looked at the FCC solutions, but they are really different from mine, so they weren’t super helpful for me fixing my code, which is what I’d rather do instead of copying the solution and just passing the challenge that way. Can you help me fix this?

I tried to include a screenshot of my code (above), and I put in one of the tests I’m not passing and showed how the console returns only an empty array. But also I’m new to these forums, so in case I messed up on including that screenshot, here is my code written out:

function whatIsInAName(collection, source) {
  let sourceKeys = Object.keys(source);
  let sourceValues = Object.values(source);
  let result = [];
  for (let i in collection) {
      if (collection[i].hasOwnProperty(sourceKeys) && collection[i][sourceKeys] === source[sourceKeys]) {
        result.push(collection[i]);
      }
  }
  return result;
}
1 Like

Can you include a link to this challenge? I’m not sure what this function is supposed to do.

Oh, yes! Totally. Sorry about that.
Wherefor Art Thou Challenge

Hello there, after many hours trying to fix this, so here is my solution:

I found that your code line 6 (if statement) does only for once like you said. Why does it happen? In that case, because at line 5 (for in loop) is working for only 3 times which is all of collection index (i = 0, 1, 2). But since we have to check every property if they are equal or not. ex: line 13, for first collection’s array object set ({“apple” : 1, “bat”: 2}). First we need to compare collection object property (“apple”) and source object property (“apple”) then both values. After that, go for (“bat”) and (“bat”) and so on. I tried so hard to fix without changing much of your code but sorry I couldn’t. So base on my above logic here’s My Solution:

 function whatIsInAName(collection, source){
  let sourceKeys = Object.keys(source);
  let sourceValues = Object.values(source);
  let result = [] ;

  for(let i in collection){
    let collectionKeys = Object.keys(collection[i]);
    let match = 0;

    for(let j = 0; j < collectionKeys.length; j++){
      if(collection[i].hasOwnProperty(sourceKeys[match]) && collection[i][collectionKeys[j]] === sourceValues[match]){
        match++;
        if(match === sourceValues.length){
          result.push(collection[i]); break;
        }
      }
    }
  }
  return result;
}

So, we need for loop to compare each object property and the loop limit is based on every collection object length, that’s why I declare new variable storing array of collection objects(collectionKeys). Next we have if condition, I changed some of yours . In that case I have to declare new variable which is “match”. It goes up every time the if condition is true, so when match equal the length of “sourceValues” then it means we take the collection object as result.

But why index of “sourceKeys[match]” and “sourceValues[match]” are value of “match”. Well, we need to move their index only when the condition is true so “match” value does. if we don’t do like that we have to put “j” as their index and it will cause error.
If we take “j” as their index then imagine when “j” value is 0 and then 1, so:
whatIsInAName([{ first: "Tybalt", last: "Capulet" }], { last: "Capulet" })

if(collection[i].hasOwnProperty(sourceKeys[1]) && collection[i][collectionKeys[j]] === sourceValues[1])

look at sourceValues[1] it will be an error because sourceValues array has only one element (index 0). That’s the reason we can’t use “j” value of for loop as their index.

I’m also an beginner of JS, so some of my explanation and my code are a bit mess, so I’m apologize for that either my English. Finally, that’s all I got for you, hope this helps :slight_smile: cheers!!

This is super helpful! I got frustrated and did other things for a day after getting stuck. So before seeing your reply, I was trying to do the nested for loops like you did to iterate through each key value pair, but I got stuck with referencing the j index. This explanation is super helpful! Thank you. Your English is not a problem at all. I thought this was really clear. It’s the JS that’s a challenge. I’m new to programming languages, and JS is my first. It’s a whole new way of thinking to wrap my brain around!

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

Oh that’s right sir, I apologize about that. I’ll be more careful next time, thank you!