Wherefore Art Thou - totally at a loss

This post might get yelled at, but I’m going to give it a shot anyway. I’ve looked at the hints and even peeked at the code, but there’s a wall of not understanding what’s going on here.

I understand what to do when you’re passed a collection, but when both the collection and the source are variables, I just don’t understand how you’re able to do anything. Does that make sense? You know source is an object, and collection is an array…but, my brain is breaking here.

How do you do anything to an object where you don’t know the slightest thing about it? It just seems so insanely beyond me. I understand once that’s all sorted out it’s just an exercise in filtering the array, but this problem is just working at a level where none of the previous challenges have operated, I guess. Any help or pointing me in any direction would be most appreciated.

I understand what to do when you’re passed a collection, but when both the collection and the source are variables, I just don’t understand how you’re able to do anything.

You treat the source the same way you would as if it were global.

How do you do anything to an object where you don’t know the slightest thing about it? I

You know about it from the instructions. Depending on where that object comes from, you may know about it from API documentation or you might have access to the code that creates the object. You can always inspect the object using the console or breakpoints to see what is actually in it.

1 Like

Thanks for your answer. OK so from the instructions I know that the object will contain a single key-value pairing. But I don’t know the key or the value. In the first example I know that the key value pair is {last: Capulet} and so I can just filter the array for that.

But that’s not what the challenge is asking for obviously, it wants me to use object methods to break open the object and find out what’s in it and use that key:value no matter what it is, right? So how does one do something like that?

Which exercise or what are you looking at?

It’s okay if you’re getting stuck. That’s what algorithm exercises are there for, to expand your way of thinking and solving problems. You’re not always going to have the data structured in the prettiest way and you’ll have to figure that out too.

2 Likes

Getting stuck is the best part even though frustrating, when you solve the problem and get that ooohhhh feeling. Feels like leveling up :stuck_out_tongue:

2 Likes

Your solution won’t depend on you knowing what keys or values you’re looking for. You will get those values from the object. There are a couple ways to get the keys of an object in JavaScript and you already know how to get the value when you have the key. All you need to know is that the first parameter is an array of objects and the second argument is an object.

1 Like

OK so is this like the OOP challenge where you make an array of properties with hasOwnProperty? I think I’m starting to break through here, thank you!!

Glad to help. It sounds like you have an idea of what to try next. Happy coding!

1 Like

Wherefore Art Thou in the Intermediate Algorithm Challenges

Yeah no I’m still totally stuck. This is what I have:

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

  for(let i = 0; i < collection.length; i++){
    console.log(Object.getOwnPropertyNames(collection[i]));
    
  }

I now have an array with the keys from source, am tempted to loop through that as well but it’s all foggy from there. I’m just trying to figure out a way to compare the keys in collection[i] with the keys in keys and I don’t know how to do it. Never mind even figuring out if the values match. This is really tough.

You not only need to compare keys, but key-value pairs

for (let key in source) {
  collection[i][key] === source[key]...
}
// Hope you will find this helpful
1 Like

This is what I have now, after looking at the solution, and reading damn near every thread on FCC about this problem, but it’s still not solving the last three examples. Can anyone explain what’s going wrong here? I’ve absolutely no idea.

function whatIsInAName(collection, source) {
  // What's in a name?
  // Only change code below this line
  var arr = [];
  var result = false;
  var keys = Object.keys(source);
  console.log(keys);
  arr = collection.filter(function(object){
    for(let i = 0; i < keys.length; i++){
      if(object.hasOwnProperty(keys[i]) && object[keys[i]] === source[keys[i]]){
        return true;
      } else {
        return false;
      }
    }
    return result;
  });
  console.log(arr);
  // Only change code above this line
  return arr;
}

You do a lot of unnecessary stuff here…

SPOILER ALERT!

function whatIsInAName(collection, source) {

  // We're filtering collection, you got this right!
  return collection.filter(o => {

    // Filter's callback expects either true or false, we'll start with true...
    let r = true;

    // We will iterate through all the keys of source argument
    for (let key in source) {

      // We will reassign r in tricky manner, so that if at least one key-value pair doesn't match, r will be false
      r = o[key] === source[key] && r;
      // If line above scares you, don't worry - it scary but totally valid. It scares me too!
    }

    // We will return r and get back to the next object in the collection, until the end when filter will eventually return filtered collection
    return r;
  });
}

It helps to look at what you’re returning compared to the expected answer. That way it’s often easier to find the patterns that lead to your logic error. Take a look.

It’s hard as hell to figure out what I’m getting back with the FCC console.log because it just says [Object ] [Object ] …so that’s I guess a sub-question I have --what do you do about that? Does switching over to repl.it help with that?

If you know it’s going to be an object, try console.table() instead of console.log()

2 Likes

That’s great! Let me give that a try :slight_smile: