2018-09-30 23:01:58 +01:00
---
id: a97fd23d9b809dac9921074f
title: Arguments Optional
isRequired: true
challengeType: 5
2020-05-21 17:31:25 +02:00
isHidden: false
2019-07-31 11:32:23 -07:00
forumTopicId: 14271
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
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, <code>addTogether(2, 3)</code> should return <code>5</code>, and <code>addTogether(2)</code> should return a function.
Calling this returned function with a single argument will then return the sum:
<code>var sumTwoAnd = addTogether(2);</code>
<code>sumTwoAnd(3)</code> returns <code>5</code>.
If either argument isn't a valid number, return undefined.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: <code>addTogether(2, 3)</code> should return 5.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(addTogether(2, 3), 5);
2020-05-21 02:26:06 -07:00
- text: <code>addTogether(23, 30)</code> should return 53.
testString: assert.deepEqual(addTogether(23, 30), 53);
- text: <code>addTogether(5)(7)</code> should return 12.
testString: assert.deepEqual(addTogether(5)(7), 12);
2018-10-20 21:02:47 +03:00
- text: <code>addTogether("http://bit.ly/IqT6zt")</code> should return undefined.
2019-07-24 01:56:38 -07:00
testString: assert.isUndefined(addTogether("http://bit.ly/IqT6zt"));
2018-10-20 21:02:47 +03:00
- text: <code>addTogether(2, "3")</code> should return undefined.
2019-07-24 01:56:38 -07:00
testString: assert.isUndefined(addTogether(2, "3"));
2018-10-20 21:02:47 +03:00
- text: <code>addTogether(2)([3])</code> should return undefined.
2019-07-24 01:56:38 -07:00
testString: assert.isUndefined(addTogether(2)([3]));
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function addTogether() {
return false;
}
addTogether(2,3);
```
</div>
</section>
## Solution
<section id='solution'>
```js
function addTogether() {
var a = arguments[0];
2018-10-08 01:01:53 +01:00
if (toString.call(a) !== '[object Number]') return;
2018-09-30 23:01:58 +01:00
if (arguments.length === 1) {
return function(b) {
if (toString.call(b) !== '[object Number]') return;
return a + b;
};
}
var b = arguments[1];
2018-10-08 01:01:53 +01:00
if (toString.call(b) !== '[object Number]') return;
2018-09-30 23:01:58 +01:00
return a + arguments[1];
}
```
</section>