I have the following challenge:
Construct a function
multiMap
that will accept two arrays – an array of values and an array of callbacks. multiMap will return an object whose keys match the elements in the array of values. The corresponding values that are assigned to the keys will be arrays consisting of outputs from the array of callbacks, where the input to each callback is the key.
The code given with the challenge:
// Uncomment these to check your work!
function uppercaser(str) { return str.toUpperCase(); }
function capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }
function repeater(str) { return str + str; }
const items = ['catfood', 'glue', 'beer'];
const functions = [uppercaser, capitalize, repeater];
console.log(multiMap(items, functions)); // should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
My Solution
function multiMap(array, callbacks) {
const emptyArray = [];
return array.reduce((acc, current) => {
array.forEach(element => {
while (callbacks.length) {
emptyArray.push(callbacks(element));
}
}
return acc[current] = emptyArray
)
}, {})
}
// Uncomment these to check your work!
function uppercaser(str) {
return str.toUpperCase();
}
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
function repeater(str) {
return str + str;
}
const items = ['catfood', 'glue', 'beer'];
const functions = [uppercaser, capitalize, repeater];
console.log(multiMap(items, functions)); // should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
My Problem:
I am trying to push myself to use methods more often in solving challenges. Every time I attempt using the reduce method, I run into syntax error. Can anyone see what I am doing wrong in this particular problem?