Wherefore art thou solution returning [ ]

Wherefore art thou solution returning [ ]
0

#1

Tell us what’s happening:
why my code is returnung [ ]

Your code so far

function whatIsInAName(collection, source) {
var arr = collection.filter(function(item){
for(var i in source){
if (source[i] != item[i]){
return false;

}
}
 });
return arr;
}
  
 
whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 });


Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36.

Link to the challenge:


Wherefore art thou problem
#2

Try adding return true after the for loop completes. I’m not sure if that’ll handle all of the tests, but it should get things back on track.


#3

Tell us what’s happening:

Your code so far

function whatIsInAName(collection, source) {
var arr = collection.filter(function(item){
for(var i in source){
if (source[i] != item[i]){
return false;

}
}
});
return arr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36.

Link to the challenge:


#4

@sa-mm told you exactly how to fix your solution. It would make it easier for you and everyone else reading your code if you would indent your code.


#5

@RandellDawson @sa-mm solution is not working .


#6

Post your latest code, so we can see if how you implemented it.


#7
function whatIsInAName(collection, source) {
  var arr = collection.filter(function(item){
    for(var i in source){
      if (source[i] != item[i]){
return true;

    }
  }
});
return arr;
}

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



#8

Your original solution was 99% correct. All you had to do was add return true after the for loop ends. Why would you want to add return true inside the if block of code? That will make your filter function keep collection items not in source.


#9

@sa-mm and @RandellDawson thanks for help its working now .


#10

@RandellDawson its working thanks for help.


#11

Hello,
In the code below, I try to create a pure function, but it doesn’t work.
I get an Error: false is not a function.
Is there anything I can do to fix it.
Thank you.

Spoiler
  function whatIsInAName(collection, source) {
      // What's in a name?
      var arr = [];

      // a pure function
     // that checks the matching proprties/values
      let getProperties = function(arr, source) {

        for (var i = 0; i < arr.length; i++) {
          for (let prop in source) {
            console.log('arr', arr[i][prop]);
            console.log('source ', source[prop]);
            if (arr[i][prop] !== source[prop]) {
              return false
            }
          }
          return true
        }
      }
     
      arr = collection.filter(getProperties(collection, source));
      return arr;
    }

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

#12

That is not how you pass a callback function to filter, you are running getProperties, not passing it as a callback.

// Check the console to see what is getting passed to the callback
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];

  // a pure function
  // that checks the matching proprties/values
  let getProperties = function (ele, index, array) {
    console.log(ele, index, array);
  }

  arr = collection.filter(getProperties);
  return arr;
}
Spoiler
// Here is version that seems to work
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];

  // a pure function
  // that checks the matching proprties/values
  let getProperties = function (ele) {
    for (const key in source) {
      if (source[key] != ele[key]) {
        return false;
      }
    }
    return true;
  }

  arr = collection.filter(getProperties);
  return arr;
}