Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

116 lines
2.1 KiB
Markdown

---
id: a97fd23d9b809dac9921074f
title: Arguments Optional
challengeType: 1
forumTopicId: 14271
dashedName: arguments-optional
---
# --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, `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:
```js
var sumTwoAnd = addTogether(2);
```
`sumTwoAnd(3)` returns `5`.
If either argument isn't a valid number, return undefined.
# --hints--
`addTogether(2, 3)` should return 5.
```js
assert.deepEqual(addTogether(2, 3), 5);
```
`addTogether(23.4, 30)` should return 53.4.
```js
assert.deepEqual(addTogether(23.4, 30), 53.4);
```
`addTogether("2", 3)` should return `undefined`.
```js
assert.isUndefined(addTogether('2', 3));
```
`addTogether(5, undefined)` should return `undefined`.
```js
assert.isUndefined(addTogether(5, undefined));
```
`addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ")` should return `undefined`.
```js
assert.isUndefined(addTogether('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
```
`addTogether(5)` should return a function.
```js
assert.deepEqual(typeof(addTogether(5)), 'function');
```
`addTogether(5)(7)` should return 12.
```js
assert.deepEqual(addTogether(5)(7), 12);
```
`addTogether(2)([3])` should return `undefined`.
```js
assert.isUndefined(addTogether(2)([3]));
```
`addTogether(2, "3")` should return `undefined`.
```js
assert.isUndefined(addTogether(2, '3'));
```
# --seed--
## --seed-contents--
```js
function addTogether() {
return false;
}
addTogether(2,3);
```
# --solutions--
```js
function addTogether() {
const first = arguments[0];
if (typeof(first) !== 'number') {
return undefined;
}
if (arguments.length === 1) {
return function(second) {
if (typeof(second) !== 'number') {
return undefined;
}
return first + second;
};
}
const second = arguments[1];
if (typeof(second) !== 'number') {
return undefined;
}
return first + second;
}
```