I am sensing I am missing smth very obvious so please hint at the mistake as to why I am not getting any output from d, but an empty array?
Any help is appreciated
// Only change code below this line
var o = Object.entries(source);
// Only change code above this line
var a = Object.entries(collection);
for(i = 0; i < Object.keys(collection).length; i++){
arr.push(collection[i]);
}
var keySource = Object.keys(source);
var d = 0;
for(var z = 0; z < arr.length; z++){
for(var j = 0; j < keySource.length; j++){
if(arr[z][keySource[j]] == source[keySource[j]]){
d.push(arr[z]);
}
}
return d;
}
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Thanks, sorry must have posted the incorrect version of the code I had initially, apologies.
That is the one I meant to post. The only thing I actually missed out on here is the return statement that came too early originally.
I can pass the first two checks, but not the last two. I can see with the output checks that my conditions are way too sloppy as any match is enough to put the element in the final d array. I guess I more or less understand what I need to change now.
Thanks for the help again! A good constructive answer. Apologies for posting the stuff that was not what I intended to post which caused a couple of extra issues you picked up on that were not meant to be there.
Cheers
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var o = Object.entries(source);
// Only change code above this line
var a = Object.entries(collection);
for(i = 0; i < Object.keys(collection).length; i++){
arr.push(collection[i]);
}
var keySource = Object.keys(source);
var d = [];
for(var z = 0; z < arr.length; z++){
for(var j = 0; j < keySource.length; j++){
if(arr[z][keySource[j]] == source[keySource[j]]){
d.push(arr[z]);
}
}
}
return d;
Thank you, of course you are right, this is the one thing every programmer should bear in mind.
Cheers