Wherefore art thou problem with last test

Tell us what’s happening:
Hello, I have problem with one test for this challenge. All other test are pass.

whatIsInAName([{“a”: 1, “b”: 2, “c”: 3}], {“a”: 1, “b”: 9999, “c”: 3}) should return

I have problem with this one I don’t know what the problem is and can’t find solution.

It seems like there is problem with loop or something and function is not comparing the items as it should. This is my guess for now.

Your code so far



function whatIsInAName(collection, source) {

  var arr = [];


  let sorNames = Object.getOwnPropertyNames(source);
  
    for (let j =0;j<sorNames.length;j++){
     
  arr = collection.filter(x=>
                        x.hasOwnProperty(sorNames[j]) &&   
                          Object.is((x)[sorNames[j]],(source)[sorNames[j]]) && 
                                Object.getOwnPropertyNames(x).length>=(sorNames.length)   ) };

  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/67.0.3396.99 Safari/537.36.

Link to the challenge:

Are you sure this is your current code that’s passing few of the tests?

Getting a syntax error with the return statement so it won’t run any tests.

This should work I edited post little bit and also added picture of the test passing with this code.

You just should stop do arr = collection.filter when it found first “false”.

If you put something like that:

for (let j =0;j<sorNames.length;j++){
       
        arr = collection.filter(x=> {
            let found = Object.is((x)[sorNames[j]],(source)[sorNames[j]]);
            console.log( found );
            return found;
        });

        console.log(arr);
    };

Yo can see it in the console.

The second iteration returns false… and the result is [], but the last one returns true and it returns: {a: 1, b: 2, c: 3}…

You just stop “filtering” in the second iteration, also feels like it should be “while” and not filter.

This is my solution: https://github.com/antonrodin/medjsalg/blob/master/whatIsInName/app.js

Is like yours but I stop then I found the first “false” in the object.

This is my modified answer for this quiz. After your comment I understood what was the problem with my code. When filter function at least one time passes all test with arguments provided it will return array no meter what even when the second argument from collection was not good. In this modified answer at the beginning there is checking if collection.length > 1 if so filter function is making its magic . If collection.length === 1 there is checking if every item in collection and source equals . Maybe I could use while as you suggested but I’m trying to work with filter and forEach and more build functions so I get use to it.

function whatIsInAName(collection, source) {

  let arr;
  /*array for checking if collection === source when collection.length =1*/
  let arrTrue = [];
  let names = Object.values(collection[0]);
  /*source values*/
  let sorValues = Object.values(source);
  /*source property names*/
  let sorNames = Object.getOwnPropertyNames(source);
  if (collection.length > 1){
  
  sorNames.forEach(function(name){ 
    
  arr = collection.filter(x=>
                        x.hasOwnProperty(name) &&   
                          Object.is((x)[name],(source)[name]) && 
                                Object.getOwnPropertyNames(x).length>=(sorNames.length)   );});}
    else {
      sorNames.forEach(function(ele){
        sorValues.forEach(function(eleme){
          if (collection[0][ele] === eleme && collection[0].hasOwnProperty(ele) && names.includes(eleme)){
            arrTrue.push(ele);
          }
        })
      })
      if (arrTrue.length === sorValues.length){
        arr = collection;
      } else {
        arr = [];
      }
    }
    return arr;
  }