Solving "Arguments Optional" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-08-05 20:28:24

---
title: Solving "Arguments Optional" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "Arguments Optional" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-035-arguments-optional
series: Solving freeCodeCamp's Algorithm Challenges
---

** *Post can also be found on [virenb.cc](https://virenb.cc/fcc-035-arguments-optional "viren's website")* **

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Arguments Optional'.



### Starter Code

```javascript
function addTogether() {
  return false;
}

addTogether(2,3);
```



### Instructions

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.

Calling this returned function with a single argument will then return the sum:

```
var sumTwoAnd = addTogether(2);
```

`sumTwoAnd(3)` returns `5`.

If either argument isn't a valid number, return undefined.



### Test Cases

- `addTogether(2, 3)` should return 5.
- `addTogether(23, 30)` should return 53.
- `addTogether(5)(7)` should return 12.
- `addTogether("http://bit.ly/IqT6zt")` should return undefined.
- `addTogether(2, "3")` should return undefined.
- `addTogether(2)([3])` should return undefined.



# Our Approach

After reading the instructions, starter code, and test cases more than once, this is what we're working with:

- We have at least one argument, a string or a number.
- We need to return a number or an `undefined`.
- We need to evaluate the argument(s). Either sum it if there is more than one number, return undefined, or return a function.



So diving straight into this...we've worked with the arguments object in a previous challenge. Looking at the test cases, we deal with different arguments (different types, etc.).



[MDN: The arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments "MDN Docs: arguments")



We can set the argument(s) into a variable or variables, depending on how many. We can use [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment "MDN Docs: deconstructing"). 



```javascript
function addTogether() {
  let [arg1, arg2] = arguments;
  console.log(`${arg1}, ${arg2}`);
}

addTogether(2, 3) // 2, 3
addTogether(2)(3) // 2, undefined
addTogether("http://bit.ly/IqT6zt") // ttp://bit.ly/IqT6zt, undefined
addTogether(2)([3]) // 2, undefined
```



So we are setting an undefined variable as `arg2`. Reading back the instructions, we have to return a function if there is only one argument.



> *If only one argument is provided, then return a function that expects one argument and returns the sum.*



Before writing that function, we should create a function to evaluate if the argument is a number. JavaScript has a built-in method to check this. It returns a true or false.



[MDN: Number.isInteger()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger "MDN Docs: isInteger()")



We can add a function like the below into `addTogether()`.



```javascript
function isNum(num) {
  return Number.isInteger(num)
}

isNum(47) // true
isNum('47') // false
```



So we have our arguments variables and now a function to check if it is a number or not. From the instructions, if an argument is not a number, return `undefined`. 



```javascript
function addTogether() {
  let [arg1, arg2] = arguments;

  function isNum(num) {
  	return Number.isInteger(num)
	}
  
  if (!isNum(arg1)) {
    return undefined;
  }
  
}
```



If we have two arguments which are both numbers, we can add them together. We can add that to an 'else if' statement.



```javascript
function addTogether() {
  let [arg1, arg2] = arguments;

  function isNum(num) {
  	return Number.isInteger(num)
	}
  
  if (!isNum(arg1)) {
    return undefined;
  }
   else if (isNum(arg1) && isNum(arg2)) {
    return arg1 + arg2;
  }
  
}
```



So if `arg1` is not a number, we have it covered. If both arguments are numbers, we add them and have that case covered. If there is only one argument, what do we do?



> If only one  argument is provided, then return a function that expects one argument and returns the sum.



```javascript
else if (!arg2) {
  // return a function
}
```



We have to return a function that expects one argument and returns the sum. 



```javascript
else if (!arg2) {
 	return function(newArg) {
		// 
  }
}
```



We have `newArg`, we have to sum it with `arg1`. But first, we must check if its a number.



```javascript
else if (!arg2) {
 	return function(newArg) {
  	if (isNum(newArg)) {
    	return arg1 + newArg;
    }
  }
}
```



This if/else statement should be able to handle all of our test cases.



# Our Solution

```javascript
function addTogether() {
  let [arg1, arg2] = arguments;

  function isNum(num) {
    return Number.isInteger(num);
  }

  if (!isNum(arg1)) {
    return undefined;
  }
  else if (isNum(arg1) && isNum(arg2)) {
    return arg1 + arg2;
  }
  else if (!arg2) {
    return function(newArg) {
      if (isNum(newArg)) {
        return arg1 + newArg;
      }
    }
  }
}
```



# Links & Resources

['Arguments Optional' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional "Intermediate Algorithm Scripting: Arguments Optional on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/035---arguments-optional.js "GitHub Solution")



Thank you for reading!