Arguments Optional Currying Issue

Hello folks, I have tried the following solution to the Arguments Optional challenge. I am passing all tests except for the currying ones - addTogether(x)(y);.

function addTogether(...arg) {
  //Using rest parameter to get an array of arguments
  //Check if both arguments are non-integer
  if (!Number.isInteger(arg[0]) || !Number.isInteger(arg[1])){
    return undefined;
  //If there are two arguments - sum them
  } else if (arg.length == 2){
      return arg[0] + arg[1];
  //If there is one argument - return a function
  } else if (arg.length == 1){
    return function(x) {
      if (!Number.isInteger(x)){
        return arg[0] + x;
      } else {
        return undefined;
      }
    }
  }
}

I get TypeError: addTogether(...) is not a function.
I cannot understand why my internal function is not working.

Edit - added link to challenge.

Could you provide a link to this challenge?

1 Like

This line is too aggressive, I think. What if you have only one argument? The second is automatically not an integer.

Side note: The tests only use integers, but your function technically should work for any numbers.

I have now added a link in the original post.

I checked any of the two arguments since the problem asked “If either argument isn’t a valid number, return undefined.”
On the side note: yes, I can use typeof per your suggestion.
But I am still unsure why addTogether(5)(7); would not work in my solution.

You have a few cases:

  1. one argument passed in, is a number

  2. one argument passed in, is not an number

  3. two arguments passed in, both are numbers

  4. two arguments passed in, at least one is not a number

1 Like

I think I understand you now . My first if statement failed my solution, because a single argument would return !Number.isInteger(arg[1]) and undefined?
Here is my passing solution:

function addTogether(...arg) {
  //Using rest parameter to get an array of arguments
  //Function checking if argument is a number
  function isNumber (a){
    if (typeof(a) == "number"){
      return true;
    } else {
      return false;
    }
  }
  //If there are two arguments - check and sum them
  if (arg.length == 2){
    if (isNumber(arg[0]) && isNumber(arg[1])) {
      return arg[0] + arg[1];
    } else {
      return undefined;
    }
  } else if (arg.length == 1){
    if (isNumber(arg[0])) {
      return (x) => {if(isNumber(x)) {
        return arg[0] + x;
      }}
    }
  }
}

Not the most elegant one, but it helped me understand the problem in a way slightly different to the other solutions.

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