Tell us what’s happening:
Your code so far
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var objLen = collection.length;
var srk = Object.keys(source);
var srkLen = srk.length;
for(var i=0;i<objLen;i++){
for(var j=0;j<srkLen;j++){
if(collection[i].hasOwnProperty(srk[j]) && collection[i][srk[j]]==Object.values(source)){
arr.push(collection[i]);
}
}
}
/*console.log();*/
console.log(arr);
// Only change code above this line
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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/wherefore-art-thou
I think i’m almost on a verge of discovering how to get around to this challenge. But something is missing. I’m checking for both quantity of keys and value they hold. It should be working, but … isn’t? It works for first two “iterations”, but when function is called and object/second parameter have more then one property, it doesn’t. As it second par … i don’t know … Discards entire first parameter …
Edit1:
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var objLen = collection.length;
var srk = Object.keys(source);
var srkLen = srk.length;
for(var i=0;i<objLen;i++){
for(var j=0;j<srkLen;j++){
if(collection[i].hasOwnProperty(srk[j]) && collection[i][srk[j]]==Object.values(source)[j]){
arr.push(collection[i]);
}
/*console.log("************************************");*/
/*console.log(srk[j]+" : "+collection[i][srk[j]]);*/
/*console.log(srk[j]+" : "+Object.values(source)[j]);*/
console.log(collection[i][srk[j]]+" : "+Object.values(source)[j]+" "+(collection[i][srk[j]]==Object.values(source)[j]));
}
}
/*console.log();*/
// Only change code above this line
/*console.log(arr);*/
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Edit2:
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var found;
var objLen = collection.length;
var srk = Object.keys(source);
var srkLen = srk.length;
for(var i=0;i<objLen;i++){
found = true;
for(var j=0;j<srkLen;j++){
if(!collection[i].hasOwnProperty(srk[j]) || collection[i][srk[j]]!=Object.values(source)[j]){
found = false;
}
if(found == false){
break;
}
/*console.log("************************************");*/
/*console.log(srk[j]+" : "+collection[i][srk[j]]);*/
/*console.log(srk[j]+" : "+Object.values(source)[j]);*/
/*console.log(collection[i][srk[j]]+" : "+Object.values(source)[j]+" "+(collection[i][srk[j]]==Object.values(source)[j]));*/
}
if(found == true){
arr.push(collection[i]);
}
}
/*console.log();*/
// Only change code above this line
console.log(arr);
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });