Where fore art thou

Tell us what’s happening:
Describe your issue in detail here.

  var arr = [];
  // Only change code below this line
let sourceObj=Object.getOwnPropertyNames(source);
console.log(sourceObj)
for(let i = 0; i < collection.length; i++){
  if (sourceObj.hasOwnProperty(collection[i])){
      arr.push(collection[i]);
  }
}
  // Only change code above this line
  return arr;
}

here is my code. i only manage to get one condition to pass. i think something is wrong with my object keys declaration . it only returns  last on console.log(sourceObj)
  **Your code so far**

function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let sourceObj=Object.getOwnPropertyNames(source);
console.log(sourceObj)
for(let i = 0; i < collection.length; i++){
if (sourceObj.hasOwnProperty(collection[i])){
    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/92.0.4515.107 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

I’ve added some comments:

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  

  // This is a confusing variable name, because
  // Object.getOwnPropertyNames() returns an array, not an object
  let sourceObj = Object.getOwnPropertyNames(source);


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

    // log out collection[i] here, it's an object
    if (sourceObj.hasOwnProperty(collection[i])) {
      arr.push(collection[i]);
    }
  }
  // Only change code above this line
  return arr;
}

Some methods that could be useful in this challenge, instead of Object.getOwnPropertyNames() :

Object.keys()
Object.values()
Object.entries()

well apparently im a bit more stumped on this challenge. can i get a hint to lead me in the right direction.

It’s a difficult challenge, so approach it slowly. You only want to push something to arr if both of these are true:

  • collection[i] has all the keys of the source object
  • the values of all those keys are equal to the values of the keys in collection[i]

Looping over collection is the right idea, but your if condition doesn’t check for the right thing. You need two checks. And you also have to keep in mind that whenever you check an item collection[i], it can only be pushed to arr if ALL the keys/values of the source match. So that would require a nested loop.

yeah im trying to go about it without looking at the solutions. much harder this way but i want to do it right. in fact i need to go back to the previous algorithms section and redo the challenges because i had the tendency to look on a few.

Looking at solutions and copy/pasting them kills your problem-solving skills, but you already know that. Use console.log a lot to see what’s actually happening in your code, grab pen and paper to lay out a solution in pseudo-code (if “this” then “that”).

thats very true. when i did look at the solution, i never copied and paste. i would just read through to learn the process of the code. and look at other articles related to the elements.

function whatIsInAName(collection, source) {
var arr = ;
// Only change code below this line
let sourceKey = Object.keys(source);
console.log(sourceKey);
arr= collection.filter(keyValue => {
for(let i = 0; i < sourceKey.length; i++){
if(!keyValue.hasOwnProperty(sourceKey[i]) || keyValue[sourceKey[i]]!==source
[sourceKey[i]]){

  console.log(keyValue[sourceKey[i]]);
  console.log(source[sourceKey[i]]);
  return false;
    
}

}return true
})

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

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

well i been doing alot of research on the filter function. and played with console.log to see how the code execute. yet i am succesful but still not 100% understanding on how and why the function worked as it did. thanks again.

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