Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
So I’m having trouble understanding this one. I don’t know what it wants exactly when passed only one arguments like the test case for addTogether(5) just says to return a function. I also don’t get how to grab the second number for cases like addTogether(5)(7) since when I check arguments only the 5 is registered. Anyway I’m just so confused with this right now so any advice on what I may be overlooking or not looking at would be amazing.

Your code so far

function addTogether() {
  
  for (args in arguments.length){
    if (!typeof Number(arguments[args])){
      return undefined;
    } else if (arguments.length == 2){
      return (arguments[0] + arguments[1]);
    } else if (arguments.length == 1) {
      var sumTwoAnd = addTogether(arguments[args]);
       return sumTwoAnd;
    }
  }
}

addTogether(2,3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

Can you explain how your recursive call is supposed to work?

I don’t think recursion would be my first choice for solving this.

I honestly wasn’t sure. The problem explanation had that for like a single arg so thats what I was thinking trying to go off of. Because I don’t quite get what it means by returning a function for a case where the arg is one value. But I also don’t know how to deal with the cases that are addTogether(3)(7) because console.log(arguments.length) will only show one argument for that.

I would suggest you forget about recursion then. This problem can easily be solved without it.

This seems to be the real issue. And I do agree, it can take a while to understand how JS throws functions around all over the place. Let’s say you have the following function:

function plusTwo(num) {
  return num + 2;
}

I’m assuming you understand that this function merely adds 2 to the number passed into it and returns that value. The function’s name is plusTwo because we used the classic method for defining a function in JS and gave it a name in the definition statement.

But we could have also done the following:

const plusFour = function (num) {
  return num + 4;
}

Technically, the function itself is anonymous, we didn’t give it an actual name, we just assigned it to the variable plusFour. But it still works the same way, we could call it as plusFour(10) and it will return 14.

So seeing as we can create anonymous functions and assign them to variables, then we can also return an anonymous function from a function just like we can return any other value from a function.

function createPlusFiveFunc() {
  return function (num) {
    return num + 5;
  }
}

// or we could use arrow notation if that makes it easier to understand

function createPlusFiveFunc() {
    return (num) => num + 5;
}

The createPlusFiveFunc function returns an anonymous function that takes one argument (a number) and then adds 5 to that number and returns the value. So now we can do:

const plusFive = createPlusFiveFunc();

After doing this, the variable plusFive points to the anonymous function returned by createPlusFiveFunc, just like the variable plusFour above pointed to the anonymous function. So now we can invoke plusFive just like any other function:

plusFive(20);

Which will return 25.

Hopefully this is enough of an explanation to get you going on solving this challenge.

Recursive might be confusing if you still new to it. Better try alternative.

Anyways, found some issues inside your code:

  1. “For in” loop should run with Array/ Object. Not a number

for (args in arguments.length){

Try" for of" / " forEach":

for (let args of arguments){}

arguments.forEach({ })

  1. This comparison ALWAYS give False.

if (!typeof Number(arguments[args]))

why not try this:

if (! Number.isInteger(args)){

  1. For a recursive function, there should be Base Case or STOP criteria. I could not identify any. Maybe you need to add base case on top of your for loop:
// Base case
  if(arguments[0] === "stop"){
    // return a function that takes input and added to arguments[1] that passed in
    return (    ) => {
       // sum new argument with arguments[1]
    }
  }

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