Wherefore art thou challenge help please

Can someone please help me with this challenge, I have been struggling a lot and just need some guidance. I dont know why I keep getting an empty array.

  **Your code so far**

function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
//look through collection. Return array of objects that have matching name and value pairs(source). Both name and value of source need to be present in collection. 
let srckey = Object.keys(source)
let key = Object.values(source)
//console.log('key:', key.join(' '))
//console.log(srckey)
//console.log('coll:', collection[2][srckey])

for(let i=0; i<collection.length; i++){
//console.log(collection[i][srckey])
if (collection[i].hasOwnProperty(srckey)){
  if (collection[i][srckey] === key){
    arr.push(collection[i])
    }
}

}

// 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/93.0.4577.82 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

Here:

let srckey = Object.keys(source)

Isn’t that going to be an array of keys? I mean, there could be more than one, right? At the very least, this is going to be an array so this:

if (collection[i].hasOwnProperty(srckey)){

isn’t going to makes sense, unless you expect the property to be an array.

I think you need to account for the fact that that is an array and may have more than one. I would expect some kind of nested loop: a loop for the collection and then a loop for the source elements.

Does that help?

Do you mean like this? I am still getting an empty array

for(let i=0; i<collection.length; i++){

  for (let j=0; j<source.length; j++){

//console.log(collection[i][srckey])

if (collection[i].hasOwnProperty(srckey[j])){

  if (collection[i][srckey[j]] === key){

    arr.push(collection[i])

    }

}

  }

}

console.log(arr)

OK, but put in some log statements there and see if you can figure out what is happening.

Being able to debug code is one of the most important things to learn as a developer. Some days I spend more time debugging that actual coding.

I have tried to, but again I am really lost. I have been at this for a few days and tried various methods I don’t understand why it doesnt work

Right, I get that. We were all where you are once: lost. Heck, I still get lost sometimes. There is nothing wrong with being confused. The skill you need to learn is to figure out how to break the code apart and to track what is happening. My first instinct is to figure out what is going on with some log statements. I would start with something like this:

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  //look through collection. Return array of objects that have matching name and value pairs(source). Both name and value of source need to be present in collection.
  let srckey = Object.keys(source);
  let key = Object.values(source);
  //console.log('key:', key.join(' '))
  //console.log(srckey)
  //console.log('coll:', collection[2][srckey])

  for (let i = 0; i < collection.length; i++) {
    console.log('checking collection element', collection[i])
    for (let j = 0; j < source.length; j++) {
      console.log('checking source key', srckey[j])

      if (collection[i].hasOwnProperty(srckey[j])) {
        if (collection[i][srckey[j]] === key) {
          arr.push(collection[i]);
        }
      }
    }
  }


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

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

Look at the output from that. Do you see anything wrong? Figure out why.

1 Like

I can see that I need to iterate through srckey.length and not source, I can also see that something is wrong with my if statement collection[i]srckey[j]

I passed the first test, but not the rest:

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  //look through collection. Return array of objects that have matching name and value pairs(source). Both name and value of source need to be present in collection.
  let srckey = Object.keys(source);
  let key = Object.values(source);
  key = key.join(' ')
  //console.log('key:', key.join(' '))
  console.log('value:', key)
  //console.log('coll:', collection[2][srckey])

  for (let i = 0; i < collection.length; i++) {
    console.log('checking collection element:', collection[i])
    for (let j = 0; j < srckey.length; j++) {
      console.log('checking source key:', srckey[j])

      if (collection[i].hasOwnProperty(srckey[j])) {
        
        if (collection[i][srckey[j]] === key) {
          arr.push(collection[i]);
        }
      }
    }
  }

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

console.log(([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }));

I’ll give you some pseudocode of how I solved this (at least, I think this does!); see if that points you in the right direction.

Create an empty array to hold matching results

Get an array of the keys from the `source`
Get an array of the values from the `source`

Loop through all items in the `collection`
    Get an array of the keys from the current item
    Get an array of the values from the current item

    If every source key is present in the current item's keys 
    and every source value is present in the current item's values
        Add the item to the return array

Return the array

When I learned to program, I had someone to get me out of ruts by giving me things like this as little cheat sheets. Eventually, I learned how to create them myself before writing any code, but it takes a lot of practice.

If you can write the algorithm out in English first, you’ll be well on your way to programming in any language. I made this pseudocode more specific to JavaScript, but you could do a more generalized version as well and solve this in any language.

Also, this is just one of infinite ways to solve this. It was the first way that came to mind, but once you solve it yourself, try and think of other ways!

What’s wrong with your if statement?

Well, first of all, it would be better to combine those two if statements into one. But that’s not your problem.

The problem is that you are calling it a match if only one of the properties/values in the source match. Is that the logic you want?

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