Stuck on Wherefore Art Thou

Stuck on Wherefore Art Thou
0

#1

Hello. I’ve been stuck on this one for a good week now and I don’t quite understand what I am doing wrong.

Below is my code - can anyone kindly point me in the right direction please?

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

// Only change code below this line
var keys = Object.keys(source);
arr = collection.filter(function(el) {
for (var i=0; i<collection.length; i++) {
collection[i].hasOwnProperty(keys);
}
});

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

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


#2

The code is not correct. To check if an object contains all the keys in the filter function you have to cycle through the keys array
For every element of the collection you need to verify for all the keys in the variabile source if the element of the collection contains the key and has the same value of the key in the source variable.
This can be a possible solution.
function whatIsInAName(collection, source) {
// What’s in a name?
var arr = [];

// Only change code below this line
var keys = Object.keys(source);
console.log(keys);
arr = collection.filter(function(el) {
for (var i=0; i<keys.length; i++) {
if(!(el.hasOwnProperty(keys[i]))){
return false;
}
}
return true;
});

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


#3

Excuse me! I copied and pasted the wrong code. This is the correct code
function whatIsInAName(collection, source) {
// What’s in a name?
var arr = [];

// Only change code below this line
var keys = Object.keys(source);
console.log(keys);
arr = collection.filter(function(el) {
var boolean = true;
for (var i=0; i<keys.length; i++) {

if(el.hasOwnProperty(keys[i]) && el[keys[i]]==source[keys[i]]){
boolean= true;
}
else{
return false;
}
}
return boolean;
});

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


#4

Thanks for that. I have to say that it doesn’t make an awful lot of sense to me, I’ve really struggled to break this one down in my mind! Next two bonfires are much simpler thankfully.


#5

Hi,
I haven’t tested this, but I think the main problem with your code is that collection[i].hasOwnProperty(keys); is not the only requirement, you need the values assigned to that property to match, too. So, add that as a condition and see if it works.
Also, you are not using filter properly. The function inside it needs to return true or false and yours is not returning anything.
Hope that helps a little.


#6

This is my solution, pretty ugly hrm.

How can I check if loop had ALL true outcomings without old-fashioned count+= ?

Also, what part in this code can be substituted for for( var in) kind of loop?
https://jsfiddle.net/f8v2LLLo/1/