1

I created a forEach function that takes an array and a callback function, and runs the callback on each element of the array. The forEach function does not return anything. There is also a separate helper function map that is used.

function forEach(array, callback) {
  for (let i = 0; i < array.length; i++) {
    callback(array[i]);
  }
}

function map(array, callback) {
  let newArr = []
  forEach(array, newArr.push(callback));
  return newArr;
}

console.log(typeof forEach); // should log: 'function'
forEach(['a', 'b', 'c'], i => console.log(i)); // should log: 'a', 'b', 'c'
console.log(typeof map); // should log: 'function'
console.log(map([3, 4, 5], n => n - 2)); // should log: [1, 2, 3]

I get the following error: Type Error on line 4: callback is not a function I'm quite close to resolving the issue but can't seem to figure out how to line 9 with the forEach function call. All test cases above pass, except the very last one.

0

3 Answers 3

2

Change your map function to this:

function map(array, callback) {
   let newArr = []
   forEach(array, (value) => newArr.push(callback(value)));
   return newArr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain how the "(value) =>" works? Trying to understand why it was running now and how rewriting it this way finally made it work. Thanks.
You may refer here to learn more about arrow functions.
1

Problem resides in forEach(array, newArr.push(callback));. Try wrapping the second parameter, newArr.push(callback), in an arrow function expression. Resulting line will be forEach(array, () => newArr.push(callback));.

Here's a working example. Check out the console log.

Good luck!

2 Comments

Your code will not work. It will not return desired value as per question.
Taking a second look, you're right. I have now edited it in order to work as intended. Thanks for the notice!
1

You just need to write return array.map(callback); and it will work.

As Array.prototype.map() map accepts callback as parameter and you are already having callback function as parameter. All you need to do is return array.map(callback);.

Similarly you can do with Array.prototype.forEach(). But forEach doesn't return anything so you can not write return array.forEach(callback);. Instead of that you need to use it as array.forEach(callback);.

function forEach(array, callback) {
  array.forEach(callback);
}

function map(array, callback) {
  return array.map(callback);
}

console.log(typeof forEach); // should log: 'function'
forEach(['a', 'b', 'c'], i => console.log(i)); // should log: 'a', 'b', 'c'
console.log(typeof map); // should log: 'function'
console.log(map([3, 4, 5], n => n - 2)); // should log: [1, 2, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.