Wrong console results

Tell us what’s happening:*
Describe your issue in detail here.
Hello. What should I change in this code for it to pass the test. The console results it brings are wrong

  **Your code so far**

function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
const allKeys = Object.keys(source);
for(let i = 0; i < collection.length; i++){for(let j = 0; j < allKeys.length; ++j)
{if(collection[i].hasOwnProperty(allKeys[j]) || collection[allKeys[j]] === source[allKeys[j]]){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" }));
  **Your browser information:**

User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1

Challenge: Wherefore art thou

Link to the challenge:

In your inner for loop :

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

you have

if (... || collection[allKeys[j]] ...)

Try adding this console.log() statement just before that if statement:

console.log(collection[allKeys[j]])

to see what’s happening there.
(Hint: compare to the code on the left side of the logical comparison operation).

Next, think about the logical comparison itself (in that same if statement).

It will return true if either the left side or the right side of the comparison is true.
Are you sure that’s what the instructions are asking for?

And, finally (I think), the instructions say:

Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array

I think that you’re including objects where any name and value pair from the source object is present.
So you’ll need to look at the logic around your inner for loop for that one.

1 Like

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