0

The function below adds arguments within 1 paranthesis fine. For example, it computes addTogether(2,3) = 5 and addTogether(2,"3") = undefined.

However, it fails to compute addTogether(2)(3) = 5, instead giving me the error that "addTogether(...) is not a function". The closure function (return function(x)) is supposed to take into the second argument in addTogether(2)(3), and I'm lost on why it dos not work.

function addTogether() {
 if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") {
 return undefined;
 } //not harmful but not necessary
 var sum = 0;
 var num = arguments[0];
 if (arguments.length === 1) {
 //if only 1 argument in the original function...
 if (typeof arguments[0] !== "number") {
 return undefined;
 //returns undefined if arguments[0] isnt a number
 }
 return function(x) {
 if (typeof arguments[0] !== "number") {
 return undefined;
 //in the closure/2nd function, if first argument isnt a number then no sum will be provided
 } else {
 sum = num + x; //x = second given argument
 return sum;
 }
 };
 }
 if (typeof arguments[0] === "number" && typeof arguments[1] === "number") {
 for (var x = 0; x < arguments.length; x++) {
 if (typeof arguments[x] === "number") {
 sum += arguments[x];
 //add the argument[0] and [1] if both are number types, not string or array types or any other types
 } else {
 sum = undefined;
 }
 }
 return sum;
 }
 // the above "if" statement is rsponsible for achieving addTogether(2,3) = 5;
}
console.log(addTogether(2)(3));

Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked Jan 23, 2017 at 6:14
5
  • 4
    if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") already causes addTogether(2) to return undefined. Commented Jan 23, 2017 at 6:18
  • ^ If you only pass a single argument, typeof arguments[1] !== "number" is true (because typeof arguments[1] will return "undefined"). Commented Jan 23, 2017 at 6:19
  • The comment "Not harmful but not necessary" is wrong. It's not necessary, and it's harmful. Commented Jan 23, 2017 at 6:20
  • @Xufox Turn that into an answer. Commented Jan 23, 2017 at 6:21
  • FWIW, you could have easily find out why the function doesn't return a function if you used your browser's developer tools and set a breakpoint at the beginning of function. Take the time to learn how to debug JavaScript. It's invaluable. Commented Jan 23, 2017 at 6:30

2 Answers 2

1

If you want your function to work like addTogether(2)(3), this means that your addTogether must take an parameter and return a function. addTogether(2) this call will return a new function, and then call the returned function with the second parameter.

In your case when you compare

if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") 

and call the function with one argument, the second typeof arguments[1] !== "number" returns you true, because the second parameter is undefined, so it is not a number and your function returns undefined.

And in your code you can remove some conditions also. Because the above condition will already check them.

function addTogether() {
 if (typeof arguments[0] !== "number") { 
 return undefined;
 } 
 var sum = 0;
 var num = arguments[0];
 if (arguments.length === 1) { 
 return function(x) {
 if (typeof arguments[0] !== "number") {
 return undefined;
 
 } else {
 sum = num + x; 
 return sum;
 }
 };
 }
 if (typeof arguments[0] === "number" && typeof arguments[1] === "number") {
 for (var x = 0; x < arguments.length; x++) {
 if (typeof arguments[x] === "number") {
 sum += arguments[x];
 
 } else {
 sum = undefined;
 }
 }
 return sum;
 }
 
}
console.log(addTogether(2)(3));

answered Jan 23, 2017 at 6:17
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help, but doesnt the (return function(x)) return that new function? Im confused how to fix this
@Pythonnoob Edited
1

if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") already causes addTogether(2) to return undefined.

Placing that if statement at the end of the function or turning that || into an && fixes it.

answered Jan 23, 2017 at 6:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.