Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
one of the functions I am supposed to pass is this :

addTogether(5)(7)
could anyone explain to me this function call? thanks

Your code so far

function addTogether() {
  let sum = 0;
  let flagOfTwo = 0;
  let flagOfOne = 0;
  let undeFlag = 0;
  for (let i = 0; i < arguments.length; i++) {
    if (arguments.length > 1) {
      sum += arguments[i]
      flagOfTwo = 1;
    }
  }
  if (flagOfTwo) {
    return sum;
  }
}
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/105.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

if addTogether(5) returns a function, that function can be called putting the (7) in front of it

What does “infront of it” mean?

You call a function adding the parenthesis after it, so like if you have a function sum, you call it sum() with any argument between the parenthesis

If a function returns a function, you can add other parenthesis to call the returned function

1 Like

lets see if I understand.
sum(2)(4) the function calls sum with (2), then it recalls it again with the result of (2), just with (4) now?

sum is the function
you call sum writing sum(2)

if sum(2) returns a function, you can call the returned function with sum(2)(4)

first of all thanks for your answer, now if we take a look at the challenge.
Shouldn’t it pass?

function addTogether() {
  let sum = 0;
  let flagOfTwo = 0;
  let flagOfOne = 0;
  if (typeof (arguments[0]) !== 'number' || typeof (arguments[1]) !== 'number') {
    return undefined;
  }
  for (let i = 0; i < arguments.length; i++) {
    if (arguments.length > 1) {
      flagOfTwo = 1;
      sum += arguments[i];
    }
    else if (arguments.length === 1) {
      function oneArg() {
        return sum += arguments[i]
      }
      return oneArg;
    }
  }
  if (flagOfTwo) {
    return sum;
  }
}

addTogether(2, 3)

I am not sure I can follow your logic

Why is this inside the loop? what’s sum and i here?

doesn’t it inherent because it is nested?

what are the values of sum and arguments and i here? could you tell?

Let’s say the function is addTogether(4)(2), what are the values there?

Sorry, I don’t know the answer console.log doesn’t work inside. which I assume because it is a function but I am not sure exactly why.
I thought it inherents outside functions?

if you call addTogether(4)(2) your function stops here, that’s why it doesn’t arrive there.

TypeError: addTogether(...) is not a function
1 Like
function addTogether() {
   if (typeof (arguments[0]) !== 'number' ) {
   return undefined;
   }
  if (arguments.length === 1) {
    function oneArg(num2) {
      if (typeof num2 !== 'number') {
  return undefined;
}
else {
  return arguments[0] + num2;
}
    }
    return oneArg;
  }
  if (typeof (arguments[1]) !== 'number') {
    return undefined;
  }
  else {
    return arguments[0] + arguments[1];
  }
}

addTogether(5)(7)

hi again, arguments[0] would you be able to tell me why it equals to 7 instead of 5?
addTogether(5)(7)

I assume you are talking about inside your returned function, oneArg? its because that’s a new function with its own arguments. When you call with addTogether(5) that turns into oneArg because you have set it to return that function. Then with the (7) after addTogether(5) like this “addTogether(5)(7)” basically what you are doing is calling oneArg(7). So its arguments would be 7.

so how can I call 5 first?
addTogether(5)(7)
I understand that (7) refers to the other function. but shouldn’t 5 be called first?

In addTogether the first argument would be 5, but like a said after that its returning the function oneArg, where its first argument would be 7. oneArg has access to variables and parameters from addTogether with closure, but with the arguments[0] inside oneArg its looking at its own arguments first. You need to somehow grab the 5 from addTogether in oneArg.

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