In order to be able to use console.log() to debug, I used Atom + Chrome’s console. The first 2 testcases seem to be passing in Chrome’s console:
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })
should return [{ first: "Tybalt", last: "Capulet" }]
.
whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })
should return [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
But they are both failing in FCC. If anyone can shed light on what I am doing wrong, I would appreciate it. (I am also failing the remaining testcases but I that makes more sense b/c I have not yet accounted for a source that has multiple properties. I would like to sort out the first 2 test-cases before moving on).
This is my code:
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
const sourceprop = Object.getOwnPropertyNames(source);
// console.log(sourceprop);
const sourceval = source[sourceprop];
// (console.log(sourceval);)
const result = collection.filter(each => each[sourceprop] === sourceval)
arr.push(result);
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Thank you!
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/