Getting stuck on Intermediate Algorithm Scripting: Wherefore art thou challenge

Tell us what’s happening:
I used spread operator on my code so If second arg has only values first arg already has, then the merged object should be the same as first arg. And Second arg will overwrite first arg properties of same name.
But I have an issue. When I run my code, I see in the output:

[ { first: 'Romeo', last: 'Montague' },
  { first: 'Mercutio', last: null },
  { first: 'Tybalt', last: 'Capulet' } ]

And I should return the objects that has the second arg which is the third object { first: "Tybalt", last: "Capulet" } .
Could you help doing that!?

My code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let code = collection.filter(function (item) {
  let obj = {...item, ...source};
  return obj;    
});

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

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 (Macintosh; Intel Mac OS X 10.16; rv:85.0) Gecko/20100101 Firefox/85.0.

Challenge: Wherefore art thou

Link to the challenge:

Challenge: Wherefore art thou
link to challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Hey @abdulrahman.mhd.anas, you sure you want to use filter as so?
Remember that filter keeps “the original” value if passes the given callback in a new array.

It’ s the map method that create a new array with the value updated.

let code = collection.filter(function (item) {
  let obj = {...item, ...source};
  return obj;    
});

/*
[ { first: 'Romeo', last: 'Montague' },
  { first: 'Mercutio', last: null },
  { first: 'Tybalt', last: 'Capulet' } ]
*/


let code = collection.map(function (item) {
  let obj = {...item, ...source};
  return obj;    
});
/*
 [ { first: 'Romeo', last: 'Capulet' },
  { first: 'Mercutio', last: 'Capulet' },
  { first: 'Tybalt', last: 'Capulet' } ]
*/

But even so, I don’t see how this should help solve your challenge :slight_smile:

Hey @Marmiz. This is old code. I update my code right now.
Actually I pass half of the challenge
My code so far!:

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

  if (Object.values(item).indexOf(...Object.values(source)) > -1) {
   arr.push(item);
}
 
});
// 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" }));

So I need help. What should I do/fix to pass the whole challenge
my test cases:

if (Object.values(item).indexOf(...Object.values(source)) > -1)

This is not what the challenge requirements are.
The challenge asks you to keep every object that has at least all keys and value from the test item.

Take this as example:

// object
{ "apple": 1, "bat": 2, "cookie": 2 }

// test
{ "apple": 1, "bat": 2 }

This one should be pass as it suffice the requirement, has all the keys and values matching from the test object (plus some extras).

While with your code above it will be discarded as cookies is not found in source.

So what to do?
What should are changes should I do to solve this problem?

My suggestion?

Stop being “clever” and try to use every “fancy” method you can think of, I fear you got yourself in a slump trying to use spread operator at all cost.

In general try to solve the challenge firsts, even with a “brute” non-elegant solution, then refactor accordingly :slight_smile:

For an head start:

collection.filter(function (item) {
// we know we have to return true or false to keep the element

// does item have all the keys in source?
// does item[key] value === source[key] value?
// if so keep it.
}

Do you know that you can take all keys of an Object into an array using Object.keys(obj)? Thant can come in handy.
Or you can loop an object using a for in loop.

Hope this helps :sparkles:

I still have a small issue
When I run my code I see array of value. not the name of it. The challenge want me to return both the name and it’s value.
My Code So Far :

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  const keys2 = Object.keys(source);
  for (var i = 0; i < collection.length; i++) {
    var obj = collection[i];
    const keys1 = Object.keys(obj);
    // ...
    if (obj.hasOwnProperty(keys2)) {
      for (let key of keys1) {
        if (obj[key] === source[key]) {
          arr.push(source[key]);
        }
      }
    }
  }

  // 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" }
  )
);

I see on the output: [ 'Capulet' ]
I want helps and tips from you developers :wink:
Thanks All!

LINK TO CHALLENGE: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

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