View on GitHub

marymafa.github.io


layout: post title: Optional arguments date: 2017-11-23 —

Optional arguments

I were asked to Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum. For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Code Explanation

  1. Create first function should add the two values and return the sum of two values.

  2. Create a function titled check numbers and it is used to checking if a number is actually a number and returns undefined if it is not. It uses typeof to check numbers.

  3. Check an arguments if it is a number then return the function expecting a second argument;

My Approach

Fistly, I created a function titled add together that will will sum two arguments together for example if is (2,5) should return 5. I have the second function titled check numbers with the sole purpose of checking if a number is actually a number and returns true. And if they any of them is undefined then return undefined. On my third test statement i said if the first argument and the second argument are type of number then add the sum of two arguments together. Lastly on my if statement i said check if the second argument is a number then add the second argument with the first argument and sums two arguments together.

Finally, it is working and it returns the sum of the two arguments and undefined if is not a number for example(“http://bit.ly/IqT6zt”, (2)([3]) should return undefined).