2021-06-15 00:49:18 -07:00
---
id: a97fd23d9b809dac9921074f
2021-07-21 20:53:20 +05:30
title: Usar argumentos opcionais
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 14271
dashedName: arguments-optional
---
# --description--
2021-07-16 11:03:16 +05:30
Crie uma função que some dois argumentos juntos. Se apenas um argumento for fornecido, então retorne uma função que espera um argumento e retorna a sua soma.
2021-06-15 00:49:18 -07:00
2021-07-16 11:03:16 +05:30
Por exemplo, `addTogether(2, 3)` deve retornar `5` e `addTogether(2)` deve retornar uma função.
2021-06-15 00:49:18 -07:00
2021-08-05 23:31:15 +09:00
Chamar essa função retornada com um argumento retornará a soma:
2021-06-15 00:49:18 -07:00
```js
var sumTwoAnd = addTogether(2);
```
2021-07-16 11:03:16 +05:30
`sumTwoAnd(3)` retorna `5` .
2021-06-15 00:49:18 -07:00
2021-07-16 11:03:16 +05:30
Se algum argumento não for um número válido, retorne undefined.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-16 11:03:16 +05:30
`addTogether(2, 3)` deve retornar 5.
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(addTogether(2, 3), 5);
```
2021-07-16 11:03:16 +05:30
`addTogether(23, 30)` deve retornar 53.
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(addTogether(23, 30), 53);
```
2021-07-16 11:03:16 +05:30
`addTogether(5)(7)` deve retornar 12.
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(addTogether(5)(7), 12);
```
2021-09-25 10:15:05 -07:00
`addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ")` deve retornar `undefined` .
2021-06-15 00:49:18 -07:00
```js
2021-09-25 10:15:05 -07:00
assert.isUndefined(addTogether('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
2021-06-15 00:49:18 -07:00
```
2021-07-16 11:03:16 +05:30
`addTogether(2, "3")` deve retornar `undefined` .
2021-06-15 00:49:18 -07:00
```js
assert.isUndefined(addTogether(2, '3'));
```
2021-07-16 11:03:16 +05:30
`addTogether(2)([3])` deve retornar `undefined` .
2021-06-15 00:49:18 -07:00
```js
assert.isUndefined(addTogether(2)([3]));
```
2022-02-16 22:48:09 +05:30
`addTogether("2", 3)` deve retornar `undefined` .
```js
assert.isUndefined(addTogether('2', 3));
```
2021-06-15 00:49:18 -07:00
# --seed--
## --seed-contents--
```js
function addTogether() {
return false;
}
addTogether(2,3);
```
# --solutions--
```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];
}
```