0

I am trying to implement a map function in javascript using reduce.

function newMap(inputArr, fn) {
    return inputArr.reduce((mapItem, elem) => 
        mapItem = (fn(elem)), [])
}

function plusTwo(num) {
    return num + 2;
}

newMap(arr, plusTwo())

console.log(newMap)

The error output is: "TypeError: fn is not a function"

So my question is -- what am I doing wrong that the reduce function signature or the function being passed is throwing an error?

P.S. -- still learning javascript, if someone could help edit the title for more clarity, that'd be much appreciated.

Thanks

7
  • There is already a map() function.... ? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 25, 2020 at 20:13
  • Yes, I understand, however I am implementing my own for the purposes of learning. Commented Aug 25, 2020 at 20:13
  • 1
    newMap(arr, plusTwo()) <= take the () off of the plusTwo Commented Aug 25, 2020 at 20:14
  • Does this answer your question? Why does click event handler fire immediately upon page load? Commented Aug 25, 2020 at 20:15
  • @Taplar, great the error went away, but now it returns a function object. I'll keep fooling around, thanks. Commented Aug 25, 2020 at 20:16

3 Answers 3

1

You're calling plusTwo when passing it, but you need to only call plusTwo inside the reduce callback. Inside the reduce callback, add the call of fn(elem) onto the accumulator array.

function newMap(inputArr, fn) {
    return inputArr.reduce((accum, item) => [...accum, fn(item)], []);
}

function plusTwo(num) {
    return num + 2;
}

const arr = [1, 2, 3];
console.log(newMap(arr, plusTwo));

Sign up to request clarification or add additional context in comments.

Comments

0
  • mapItem is the reference to the resulting array. You need to push the resulting value to it instead of reassigning it in every iteration, ie mapItem.push(fn(elem))
  • Pass in plusTwo as a function instead of invoking it plusTwo(), this is why you were getting the fn is not a function error
  • Print the result of newMap instead of newMap itself
function newMap(inputArr, fn) {
  return inputArr.reduce((mapItem, elem) => {
      mapItem.push(fn(elem))
      return mapItem;
  }, [])
}

function plusTwo(num) {
    return num + 2;
}

let result = newMap([1,2,3], plusTwo)

console.log(result)

Comments

0

The callback you pass to reduce is expected to return something. Plus what others commented, you want to pass plusTwo, not plusTwo(), which is a NaN (the result of undefined+2):

function newMap(inputArr, fn) {
    return inputArr.reduce((accum, item) => {
        accum.push(fn(item));
        return accum;
    }, []);
}

function plusTwo(num) {
    return num + 2;
}

const arr = [1, 2, 3];
console.log(newMap(arr, plusTwo));

console.log("plusTwo:", plusTwo);
console.log("plusTwo():", plusTwo());

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.