Arguments Optional - addTogether(…) is not a function

Tell us what’s happening:
I can’t work this one out… when I try to run addTogether(2)(3) I get this error:
“TypeError: addTogether(…) is not a function”
I think the else statement is what is being asked for?

Your code so far


function addTogether() {
  let args = [...arguments];
  let result = 0;
  let x = 0;
  args.map(num => {
    if (typeof num === "string") {
      result = undefined;
    } else if (args.length > 1) {
      x += num;
      result = x;
    } else {
      result = addTogether(args[0], 3);
    }
  })
  return result;
}

addTogether(2)(3)
addTogether(2)([3])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

Link to the challenge:

I think it’s meant to be run like:
addTogether(2, 3);
not:
addTogether(2)(3);

hth

Two of the tests are:
addTogether(2)(3) should return 5.
addTogether(2)([3]) should return undefined.

1 Like

I’m an idiot. I forgot this one.
iirc, you have to return a function. Try something like this in your last else (code from memory)

result = function(next) {
  return args[0]+next;
}

Edit: Also test ‘next’ for being a number etc

4 Likes

First of all… you’re not an idiot! You can’t expect to remember everything!

I think I understand this now… does the word function know that it’s supposed to call this (addTogether()) function again?

lol, thanks, but i’m an idiot for only spending 1 second looking at your question before giving a wrong answer - something I’ve done before so i should know better :wink:

(To help with clarity, when I say 'method', I mean `addTogether()`, when i say 'function' i mean the bit that i put in my last answer - even though they are both 'functions')

Anyhoo, no, the function keyword doesn’t run addTogether() again - the function is the thing/object returned from the call to addTogether().

If you call the method with two numbers, it returns a number (the sum of those two numbers), but if you call it with only one number, it returns the function. That function is created with the single number that was provided (to addTogether()) and expects a(nother) single number which it adds to its already stored number (the first one).

Hope that makes sense. Returning functions is a bit trickier than say, returning a number, or a string, but since functions are objects, they can be returned just like any other objects

1 Like