-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArguments_Optional.js
More file actions
62 lines (58 loc) · 1.79 KB
/
Arguments_Optional.js
File metadata and controls
62 lines (58 loc) · 1.79 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function addTogether() {
if (arguments.length === 2 &&
typeof arguments[0] === "number" &&
!Number.isNaN(arguments[0]) &&
typeof arguments[1] === "number" &&
!Number.isNaN(arguments[1])) {
return arguments[0] + arguments[1];
}
if (arguments.length === 1 &&
typeof arguments[0] === "number" &&
!Number.isNaN(arguments[0])) {
var arg0 = arguments[0];
return function() {
if (typeof arguments[0] === "number" &&
!Number.isNaN(arguments[0])) {
return arg0 + arguments[0];
}
};
}
}
/* I didn't really like the flow of the code below.
So, I refactored to what is above. */
/* This is my refactor of the first:
function addTogether() {
var x = arguments[0];
if (typeof x !== "number") return undefined;
if (arguments[1] !== undefined) {
var y = arguments[1];
return (typeof y !== "number") ? undefined : x + y;
}
return function(y) {
return (typeof y !== "number") ? undefined : x + y;
};
}
*/
/* This is my first push to get the lesson completed.
function addTogether() {
var x = arguments[0];
if (typeof x !== "number") return undefined;
if (arguments[1] !== undefined) {
var y = arguments[1];
if (typeof y !== "number") {
return undefined;
} else {
return x+y;
}
}
return function(y) {
if (typeof y !== "number") return undefined;
return x + y;
};
}
*/
addTogether(2, 3); // should return 5.
addTogether(2)(3); // should return 5.
addTogether("http://bit.ly/IqT6zt"); // should return undefined.
addTogether(2, "3"); // should return undefined.
addTogether(2)([3]); // should return undefined.