Closures Function - Arguments Optional algo challenge

Hi,

I’m trying to understand how those functions with 2 sets of parenthesis work with closure.

Beside getting into the closure, i would like to understand why I can’t get just the value of the first parenthesis? it always gives me an error message after, like it can’t read what is after. Which is fine because I just want the value from the first parenthesis!

Here is my code:

function addTogether() {
  
  console.log(arguments[0]);
    
}

addTogether(2)(3);

I am expecting 2 as an answer.

Same when I’m trying to reuse some part of the basic answer of the challenge to test if arguments[0] is a number or not.

function addTogether() {
  
  var c = arguments[0];
  
  if (typeof c == number){
    console.log('yes')
  }
  else{
    console.log('no')
  }
  
  
}

addTogether(2)(3);

All I get is an error. So I wonder how the solution for the challenge works if I can’t even get the number value in the first argument.

Anyone can help me on this part?!

Thanks!

Hi Randell,

Yes it does, but when trying to check if number or not, the error is still passed right? that’s why I can’t get a yes or no answer? or it is for something different?

So i can’t just take the number of the first arguments without having the error after? How can I store the first argument in a variable without having the error?

i’m sorry fr all those questions, but it is bugging me :slight_smile:

typeof returns "number" for a number. You need the "s in your condition.

1 Like

Ok thank you guys, I think i got a quick understanding of the whole concept.

I’m still struggling to understand why I can’t just receive the number as an answer without the following error message when trying independently of the addTogether function.

Thank you!

Hi Randell,

Here is the error message

function addTogether() {
  
  console.log(arguments[0]);

}

addTogether(2)(3);

error message from JSBIN:
“error”
"@payopavoji.js:21:1
l</b.render/<@https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13922
k</a.use/<@https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866
"

even entered like this I have no answer:

function addTogether() {
  
return arguments[0]
  
}

console.log(addTogether(2)(3));

error message: from JSBIN:
“error”
"@kaxeweqagu.js:20:38
l</b.render/<@https://static.jsbin.com/js/prod/runner-4.1.3.min.js:1:13922
k</a.use/<@https://static.jsbin.com/js/prod/runner-4.1.3.min.js:1:10866
"