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.
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
(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