Help in Solving Arguments Optional Requested

Help in Solving Arguments Optional Requested
0

#1

Hello. I am working on Arguments Optional in the intermediate scripting section. https://www.freecodecamp.org/challenges/arguments-optional

I am receiving the error message: “TypeError: Num is not a Function”. It is saying that variables first and second are used out of scope. Could I get some help with this?


function addTogether() {
  //turn object into array
  var nums = [].slice.call(arguments);
  //call a function on every item in array
  if (!nums.every(function(num){
    //if the array contains only numbers, return numbers
    //Otherwise, return undefined
    return typeof(num) =='number' ? num : undefined; 
  }))
    //assign first and second nums in array to variables
  var first = nums(arguments[0]);
  var second = nums(arguments[1]);
  if (arguments.length==2) {
    return first + second;
  } else {
    return undefined;
  }
  if (first) {
    return function(third) {
      return first + third;
    };
  } else {
    return undefined;
  }
  return false;
}

addTogether(2,3);

#2

nums is an array. You have the wrong syntax when trying to reference an array element below:

var first = nums(arguments[0]);

arguments is an “array-like” object and you are using [ ] to reference a specific element. You need to use [ ] with nums also.


#3

I am now getting 5 as the output; however, I’m still getting Xs for addTogether(2)(3) should return 5. and addTogether(2)([3]) should return undefined. I don’t know how to read them.

Here is my updated code:

function addTogether() {
  //turn object into array
  var nums = [].slice.call(arguments);
  //call a function on every item in array
  if (!nums.every(function(num){
    //if the array contains only numbers, return numbers
    //Otherwise, return undefined
    return typeof(num) =='number';
  })) 
    return undefined;
    //assign first and second nums in array to variables
  var first = nums[0];
  var second = nums[1];
  if (nums.length===2) {
    return first + second;
  } else {
    return undefined;
  }
  if (first) {
    return function(obj) {
      return first + obj;
    };
  } else {
    return undefined;
  }
}

addTogether(2,3);

#4

Use the browser’s console (Ctrl+Shft+J in Chrome) to see the errors you are getting. Always start at the console whenever you are not getting the results you expect. An error will stop the script from running at the point the error is encountered. You will probably need to put some console.log statements in to see the values of variables as it is executing for the addTogether(2)(3) and the addTogether(2)[3]).


#5

What you are not understanding is the addTogether(2) part of the addTogether(2)(3) returns undefined, so when the (3) is on the end, JavaScript see undefined(3). There is no function call undefined, hence the error message. If you only receive one argument (in the case of addTogether(2)) you are supposed to returns a function that expects one argument and returns the sum. You need to modify your first else block of code so that the function I describe above is returned instead of undefined (what you are returning).


#6

Try putting breakpoint on your JS code in the browser and press F10 key to debug code line by line.


#7

I’ve realized I have not learned how to debug properly. Do you know of any good tutorials that teach this? Thanks!