Tell us what’s happening:
Please ignore most of the console.log commands in my code, I used it for testing (I can also edit them out if neccessary). (Edit: Deleted them)
I always get an undefined array as a result and I’m not sure where my mistake is in the code. I’m checking first if the keys from the source object are in the elem object (elem is each array entry from collection), then I check if one of the values is equal to one of the values from source object. If both of this is true, the elem object gets pushed into the accumulator “arr”.
Your code so far
function whatIsInAName(collection, source) {
var keyArrSrc = Object.keys(source)
collection.reduce((arr, elem) =>{
if(elem.hasOwnProperty(keyArrSrc)){
let tmpArrVal1 = Object.values(elem)
let tmpArrVal2 = Object.values(source)
for(let i = 0; i<tmpArrVal1.length; i++){
for(let j = 0; j<tmpArrVal2.length; j++){
if(tmpArrVal1[i] == tmpArrVal2[j]){
arr.push(elem)
}
}
}
}
return arr
}, [])
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41
Challenge: Intermediate Algorithm Scripting - Wherefore art thou
Link to the challenge: