when the function is being called the keys (lisa, homer, etc) I know should be passed in slightly differently than it is currently, but I tried many things and I can’t figure out how to get it to return anything other than undefined
The expected console output should be:
// BAAAAAART! // Eat My Shorts! // d’oh!
// Returns undefined
Also, I’ve tried using console.log(jsObject[key]); within the for loop but still got undefined, so used [keys] instead which works but ignores the loop. Really struggling with this one!
let printValuesOf = (jsObject, keys) => {
for (let i = 0; i <= keys.length; i++) {
let key = keys[i];
console.log(jsObject[key]);
}
}
let simpsonsCatchphrases = {
lisa: 'BAAAAAART!',
bart: 'Eat My Shorts!',
marge: 'Mmm~mmmmm',
homer: 'doh!',
maggie: '(Pacifier Suck)',
};
printValuesOf(simpsonsCatchphrases, 'lisa', 'bart', 'homer');
How would you fix this to pass the function? Thanks a lot for any help, and sorry for this I am just a a beginner
>Solution :
keys is not an array, keys is 'lisa', you wanted to use ... operator
let printValuesOf = (jsObject, ...keys) => {
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
console.log(jsObject[key]);
}
}
let simpsonsCatchphrases = {
lisa: 'BAAAAAART!',
bart: 'Eat My Shorts!',
marge: 'Mmm~mmmmm',
homer: 'doh!',
maggie: '(Pacifier Suck)',
};
printValuesOf(simpsonsCatchphrases, 'lisa', 'bart', 'homer');