-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadditionWithOptionalArguments.js
More file actions
45 lines (35 loc) · 1.48 KB
/
additionWithOptionalArguments.js
File metadata and controls
45 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
== Problem ==
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:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3) returns 5.
If either argument isn't a valid number, return undefined.
== Test Cases ==
- addTogether(2, 3) should return 5.
- addTogether(23, 30) should return 53.
- addTogether(5)(7) should return 12.
- addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ") should return undefined.
- addTogether(2, "3") should return undefined.
- addTogether(2)([3]) should return undefined.
- addTogether("2", 3) should return undefined.
- addTogether(5, undefined) should return undefined.
*/
function addTogether() {
const [first, second] = arguments;
// Check if first argument is valid, if not return undefined.
if (typeof first !== 'number') {
return undefined;
}
// Check if there is only 1 argument provided. If yes return a function which accepts another argument to add to the first argument.
if (arguments.length === 1) {
return (y) => addTogether(first, y);
}
// If there is more than 1 argument, check if the second one is valid.
if (typeof second !== 'number') {
return undefined;
}
// finally return the desired result for the case with 2 arguments.
return first + second;
}