2018-10-10 22:03:03 +00:00
---
id: a97fd23d9b809dac9921074f
2021-02-06 04:42:36 +00:00
title: Arguments Optional
2018-10-10 22:03:03 +00:00
challengeType: 5
2020-09-07 08:10:29 +00:00
forumTopicId: 14271
2021-01-13 02:31:00 +00:00
dashedName: arguments-optional
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00: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-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
For example, `addTogether(2, 3)` should return `5` , and `addTogether(2)` should return a function.
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
Calling this returned function with a single argument will then return the sum:
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
`var sumTwoAnd = addTogether(2);`
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`sumTwoAnd(3)` returns `5` .
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
If either argument isn't a valid number, return undefined.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`addTogether(2, 3)` should return 5.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.deepEqual(addTogether(2, 3), 5);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`addTogether(23, 30)` should return 53.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
2021-01-12 16:18:51 +00:00
assert.deepEqual(addTogether(23, 30), 53);
2020-12-16 07:37:30 +00:00
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`addTogether(5)(7)` should return 12.
2021-01-12 16:18:51 +00:00
```js
assert.deepEqual(addTogether(5)(7), 12);
```
2021-02-06 04:42:36 +00:00
`addTogether("http://bit.ly/IqT6zt")` should return undefined.
2020-12-16 07:37:30 +00:00
```js
assert.isUndefined(addTogether('http://bit.ly/IqT6zt'));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`addTogether(2, "3")` should return undefined.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.isUndefined(addTogether(2, '3'));
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`addTogether(2)([3])` should return undefined.
2020-09-07 08:10:29 +00:00
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.isUndefined(addTogether(2)([3]));
2018-10-10 22:03:03 +00:00
```
2020-08-13 15:24:35 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --seed-contents--
```js
function addTogether() {
return false;
}
addTogether(2,3);
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
function addTogether() {
var a = arguments[0];
if (toString.call(a) !== '[object Number]') return;
if (arguments.length === 1) {
return function(b) {
if (toString.call(b) !== '[object Number]') return;
return a + b;
};
}
var b = arguments[1];
if (toString.call(b) !== '[object Number]') return;
return a + arguments[1];
}
```