forked from zero-to-mastery/JS_Fun_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluigi_solutions.js
67 lines (56 loc) · 1.62 KB
/
luigi_solutions.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
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
// Write a function identity that takes an argument and returns that argument
const identity = (arg) => arg;
//Write a binary function addb that takes two numbers and returns their sum
const addb = (a, b) => a + b;
// Write a binary function subb that takes two numbers and returns their difference
const subb = (a, b) => a - b;
// Write a binary function mulb that takes two numbers and returns their product
const mulb = (a, b) => a * b;
// Write a binary function minb that takes two numbers and returns the smaller one
const minb = (a, b) => (a < b ? a : b);
// Write a binary function maxb that takes two numbers and returns the larger one
const maxb = (a, b) => (a > b ? a : b);
// Write a function add that is generalized for any amount of arguments
const add = (...nums) => {
return nums.reduce((a, acc) => {
return a + acc;
});
};
// Write a function sub that is generalized for any amount of arguments
const sub = (...nums) => {
return nums.reduce((a, acc) => {
return a - acc;
});
};
//Write a function mul that is generalized for any amount of arguments
const mul = (...num) => {
return num.reduce((a, acc) => {
return a * acc;
}, 1);
};
// Write a function min that is generalized for any amount of arguments
const min = (...num) => {
return num.reduce((a, b) => Math.min(a, b));
};
// Write a function max that is generalized for any amount of arguments
const max = (...num) => {
return num.reduce((a, b) => Math.max(a, b));
};
module.exports = {
identity,
addb,
subb,
mulb,
minb,
maxb,
add,
sub,
mul,
min,
max,
addRecurse,
mulRecurse,
minRecurse,
maxRecurse,
fill,
};