2018-09-30 23:01:58 +01:00
---
id: a97fd23d9b809dac9921074f
title: Arguments Optional
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 14271
2021-01-13 03:31:00 +01:00
dashedName: arguments-optional
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
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.
2020-11-27 19:02:05 +01:00
For example, `addTogether(2, 3)` should return `5` , and `addTogether(2)` should return a function.
2018-09-30 23:01:58 +01:00
Calling this returned function with a single argument will then return the sum:
2020-11-27 19:02:05 +01:00
`var sumTwoAnd = addTogether(2);`
`sumTwoAnd(3)` returns `5` .
2018-09-30 23:01:58 +01:00
If either argument isn't a valid number, return undefined.
2020-11-27 19:02:05 +01:00
# --hints--
`addTogether(2, 3)` should return 5.
```js
assert.deepEqual(addTogether(2, 3), 5);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`addTogether(23, 30)` should return 53.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(addTogether(23, 30), 53);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`addTogether(5)(7)` should return 12.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(addTogether(5)(7), 12);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`addTogether("http://bit.ly/IqT6zt")` should return undefined.
```js
assert.isUndefined(addTogether('http://bit.ly/IqT6zt'));
```
`addTogether(2, "3")` should return undefined.
```js
assert.isUndefined(addTogether(2, '3'));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`addTogether(2)([3])` should return undefined.
```js
assert.isUndefined(addTogether(2)([3]));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function addTogether() {
return false;
}
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
addTogether(2,3);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```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];
}
```