Not understanding this challenge

Tell us what’s happening:
I don’t know what the addTogether(2)(3) should equal 5; I think I have all the code to make that happen, but I don’t know what really happened.

Your code so far


function addTogether() {
let isInteger = true;
let argadder = arguments[0];
for(let i = 0; i < arguments.length; i++) {
if(typeof arguments[i] !== "number") {
isInteger = false;
}
}
if(isInteger === true) {
if(arguments.length === 2) {
  return (arguments[0] + arguments[1]);
} else if (arguments.length === 1) {
  return(function(arg) {
  return(arg[0]) + argadder;
  }
  )
}
}
}

addTogether(2);

Your browser information:

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

Challenge: Arguments Optional

Link to the challenge:

function addTogether() {
  // Check if the parameter is legal
  const check = function(args) {
    for (const arg of args) {
      if (typeof arg !== 'number') {
        return false
      }
    }

    return true
  }

  if(!check(arguments)) {
    return
  }

  const LENGTH = arguments.length

  // Only one parameter, should return a function
  if (LENGTH === 1) {
    const TEMP = arguments[0]
    return function(value) {
      if(!check(arguments)) {
        return
      }

      return TEMP + value
    }
  } else if (LENGTH === 2) {
    // Two parameters, return the result of adding the two parameters
    const [a, b] = arguments
    return a + b
  }
}

maybe it can help you

@Lninn I have blurred your code.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

2 Likes

why are you using arg[0] here?
if arg is a number, it doesn’t have anything inside

Ohhhh *facepalms thank you I think I messed that up.