-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg19-argumentsOptional.js
More file actions
111 lines (96 loc) · 3.13 KB
/
alg19-argumentsOptional.js
File metadata and controls
111 lines (96 loc) · 3.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const assert = require('assert');
/*
Source: <https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional>
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 myAddTogether(...num1) {
const checkNumber = (n) => typeof n === 'number';
if (!num1.every((num) => checkNumber(num))) {
return undefined;
}
if (num1.length > 1) {
return num1.reduce((acc, curr) => acc + curr);
}
return function (num2) {
return (checkNumber(num2) ? num1[0] + num2 : undefined);
}
}
myAddTogether(2, 3);
assert.strictEqual(myAddTogether(2, 3), 5);
assert.strictEqual(myAddTogether(23, 30), 53);
assert.strictEqual(myAddTogether(5)(7), 12);
assert.strictEqual(myAddTogether("http://bit.ly/IqT6zt"), undefined);
assert.strictEqual(myAddTogether(2, "3"), undefined);
assert.strictEqual(myAddTogether(2)([3]), undefined);
/*
Get a help > Get a hint <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-arguments-optional/14271>
*/
//Solution 1
function addTogether1() {
// Function to check if a number is actually a number
function checkNum(num) {
return (typeof num === "number");
};
if (arguments.length === 2) {
// Check if we have two arguments and if they are numbers
// Return the sum if they are both numbers
let first = arguments[0];
let second = arguments[1];
if (checkNum(first) && checkNum(second)) {
return first + second;
} else {
return undefined;
}
} else if (arguments.length === 1) {
// If only one argument was found, return a new function
let first = arguments[0];
if (checkNum(first)) {
// Return function that expect a second argument.
function addSecond(second) {
// Check if the new argument is a number
if (checkNum(second)) {
return first + second;
} else {
return undefined;
}
};
return addSecond;
} else {
return undefined;
}
} else {
// Incorrect number of arguments found
return undefined;
}
}
//Solution 2
function addTogether2() {
const [first, second] = Object.values(arguments);
// Check first argument
if (typeof first !== "number") {
return undefined;
}
// Function to add second argument
const addSecond = (second) => typeof second === "number" ? first + second : undefined;
// Check second argument
if (second !== undefined) {
return addSecond(second);
} else {
return addSecond
}
}
//Solution 3
function addTogether3() {
var args = Array.from(arguments);
return args.some(n => typeof n !== "number")
? undefined
: args.length > 1
? args.reduce((acc, n) => (acc += n), 0)
: n => (typeof n === "number" ? n + args[0] : undefined);
}