Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.
why addTogether(5)([7]) ok, but addTogether([5])(7) return TypeError: addTogether(…) is not a function. Can you explain to me?

   **Your code so far**
function addTogether(...args) {
 const [first, second] = args;
 if (args.length === 1 && typeof first === 'number') {
   return num => {
     if (typeof num === 'number') {
       return first + num;
 if (typeof first === 'number' && typeof second === 'number') {
   return first + second;
let t= addTogether([5])(7);

   **Your browser information:**

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

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

@mrngochoi89 Hey,
First, a very good question. I had to spend some time to identify what went wrong. So, as you can see, if you called addTogether() with one argument, it should return a function. But here,

addTogether([5]) ===> return undefined

And undefined is not a function. If you want to call the addTogther() with second parameter, you have to return a function from the first parameter. In this case, since addTogether([5]) is not returning a function, just retuning undefined you get the

TypeError: addTogether(...) is not a function

When it tries to call the second parameter.
Hope you understand! :grinning:

Good code for this challenge! Your answer is pretty much correct. the best way to think about why this isn’t working is looking at the typeof what is in the code
let t= addTogether([5])(7);
if you console.log(typeof [5]) it’ll say object, as an array in JS is really a type of object (not to get into that right not but it is).

This means when your code tests it, it fails the first conditional statement: it isn’t a number so it never returns a function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.