-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargumentsOptional.js
40 lines (38 loc) · 1.22 KB
/
argumentsOptional.js
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
/**
* https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional
* 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.
*/
function addTogether(num1, num2) {
if (typeof num1 !== 'number') {
return undefined
}
console.log(typeof num2 === 'number')
if (typeof num2 === 'number') {
return num1 + num2
} else {
if (num2) {
return undefined
}
return function(newNum) {
if (typeof newNum === 'number') {
return num1 + newNum
}
return undefined
}
}
}
addTogether(2, 3) // 5.
addTogether(2)(3) // 5.
addTogether("http://bit.ly/IqT6zt") // undefined.
addTogether(2, "3") // undefined.
addTogether(2)([3]) // undefined.