Needless to say that I’ve spent two days trying to figure this out and I can’t get my head around it.
First the Object.keys was new and I read all I could on the subject but I still don’t understand it
Next, I’ve added in one of the three conditions that are still failing. This one is supposed to return [].
But the way I’ve written my code I’m still generating {object, Object] instead of an empty array.
Can anyone please explain this lesson and where I went wrong?
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var skeys = Object.keys(source);
for (var i = 0; i < collection.length; i++){
var j = skeys.length - 1;
if (collection[i][skeys[j]] === source[skeys[j]]){
arr.push(collection[i]);
}
}
console.log(arr);
// Only change code above this line
return arr;
}
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/
Thanks
I’ll beat it to death for another day and get back to you :-s
I tried making j a for loop as well but I couldn’t work out how to build the result.
Well I abandoned the last code I just wasn’t getting it. So I tried a filter to sort out my keys but it still doesn’t work. I got it to work with a code solution which I’ve commented out but I want to work through this on my own.
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var skeys = Object.keys(source);
/*return collection.filter(function(newCol){
return skeys.map(function(key){
return newCol.hasOwnProperty(key) && newCol[key] === source[key];
})
.reduce((a, b) => a && b);
});*/
return collection.filter(function(key){
for (var i = 0; i < skeys.length; i++){
if (key.hasOwnProperty(skeys[i]) || key[skeys[i]] === source[skeys[i]]){
return true;
}
else return false;
}
});
console.log(arr);
// Only change code above this line
return arr;
}
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})
Thanks
When sKeys[1] = b shouldn’t it fail since key[sKeys[1]] and source[keys[1]] are not equal?
I’m passing the first three tests when I changed the || to && but the last three tests fail
Yes thanks
When I changed it I passed the first test as well as the second two but I’m still failing on the last three
So both conditions are required to pass the test for hasOwnProperty
I don’t get why I’m returning collection and not comparing with source?
Well…after three days I checked the solutions and found one that does pretty much the same as mine but it checks for the keys and properties don’t match. At least I learned a lot of new functions, hopefully the next test is more straightforward.
Thanks Randell for your help
Final code:
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var sKeys = Object.keys(source);
/*return collection.filter(function(newCol){
return skeys.map(function(key){
return newCol.hasOwnProperty(key) && newCol[key] === source[key];
})
.reduce((a, b) => a && b);
});*/
return collection.filter(function(key){
for (var i = 0; i < sKeys.length; i++){
if (!key.hasOwnProperty(sKeys[i]) || key[sKeys[i]] !== source[sKeys[i]]){
return false;
}
}
return true;
});
/*return collection.filter(function (key) {
for(var i = 0; i < sKeys.length; i++) {
if(!key.hasOwnProperty(sKeys[i]) || key[sKeys[i]] !== source[sKeys[i]]) {
return false;
}
}
return true;
});*/
console.log(arr);
// Only change code above this line
return arr;
}
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})
1 Like