-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathr0keys_solutions.js
More file actions
151 lines (109 loc) · 5.1 KB
/
r0keys_solutions.js
File metadata and controls
151 lines (109 loc) · 5.1 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// A function (identity) that takes an argument and returns that argument
// e.g. indentity("Hello") // Hello
const identity = (arg) => arg;
// A binary function (addb) that takes two numbers and returns their sum
// Bonus: both arguments have to be numbers, if they are not the function will return an error to the user
// e.g. addb(1, "2") // Sorry, both parameters must be numbers
// addb(1, 2) // 3
const addb = (num1, num2) => {
return (typeof num1 !== "number" && typeof num2 !== "number") ? `Sorry, both parameters must be numbers` : num1 + num2;
};
// A binary function (subb) that takes two numbers and returns their difference
// Bonus: same as above, return an error if the user inputs unintended parameters
// e.g. subb(3, false) // Sorry, both parameters must be numbers
// subb(8, 13) // -5
const subb = (num1, num2) => {
return (typeof num1 !== "number" && typeof num2 !== "number") ? `Sorry, both parameters must be numbers` : num1 - num2;
};
// A binary funciton (mulb) that takes two numbers and returns thir product
// Bonus: same again as above
// e.g. mulb(6, null) // Sorry, both parameters must be numbers
// mulb(6, 6) // 36
const mulb = (num1, num2) => {
return (typeof num1 !== "number" && typeof num2 !== "number") ? `Sorry, both parameters must be numbers` : num1 * num2;
}
// A binary function (minb) that takes two numbers and returns the smaller one
// Bonus: same again as above
// e.g. minb(3, "yes") // Sorry, both parameters must be numbers
// minb(5, 2) // 2
const minb = (num1, num2) => {
return (typeof num1 !== "number" && typeof num2 !== "number") ? `Sorry, both parameters must be numbers`
: (num1 < num2 ? num1 : num2);
};
// A binary function (maxb) that takes two numbers and returns the bigger one
// Bonus: same again as above
// e.g. maxb(5, true) // Sorry, both parameters must be numbers
// maxb(4, 3) // 12
const maxb = (num1, num2) => {
return (typeof num1 !== "number" && typeof num2 !== "number") ? `Sorry, both parameters must be numbers`
: (num1 > num2 ? num1 : num2);
};
// A function (add) that is generalized for any number of arguments
// Bonus: same again as above
// e.g. add(3, 4, 5, 6, true) // Sorry both parametes must be numbers
// add(3, 4, 5, 6, 7) // 25
const add = (...nums) => {
return (nums.some(isNaN)) ? `Sorry, all parameters must be numbers` // nums.some(<some_condition>) will return a boolean if some values meet the condition
: nums.reduce((accumilator, currentVal) => accumilator + currentVal);
};
// A function (sub) that is generalized for any number of arguments
// Bonus: same again as above
// e.g. sub(4, 1, 2, 3) // Sorry, all parameters must be numbers
// add(4, 1, 2, 3) // -2
const sub = (...nums) => {
return (nums.some(isNaN)) ? `Sorry, all parameters must be numbers`
: nums.reduce((accumulator, currentVal) => accumulator - currentVal);
};
// A function (mul) that is generalized for any number of arguments
// Bonus: same again as above
// e.g. mul(4, 4, false) // Sorry, all parameters must be numbers
// mul(4, 4, 3) // 48
const mul = (...nums) => {
return (nums.some(isNaN)) ? `Sorry, all parameters must be numbers`
: nums.reduce((accumulator, currentVal) => accumulator * currentVal)
};
// A funciton (min) that is generalized for any number of arguments
// Bonus: same again as above
// e.g. min(3, 4, 6, undefined) // Sorry, all parameters must be numbers
// min(3, 4, 5, 1) // 1
const min = (...nums) => {
return (nums.some(isNaN)) ? `Sorry, all parameters must be numbers`
: nums.reduce((accumulator, currentVal) => accumulator > currentVal ? accumulator = currentVal : accumulator = accumulator)
};
// A funciton (max) that is generalized for any number of arguments
// Bonus: same again as above
// e.g. max(2, 5, 6, true) // Sorry, all parameters must be numbers
// max(2, 10, 4) // 10
const max = (...nums) => {
return (nums.some(isNaN)) ? `Sorry, all parameters must be numbers`
: nums.reduce((accumulator, currentVal) => accumulator < currentVal ? accumulator = currentVal : accumulator = accumulator)
};
// A function (addRecurse) that is the generalized (add) function but uses recursion
// Bonus: same again as above
// e.g. addRecurse(3, 2, 5, true) // Sorry, all parameters must be numbers
// addRecurse(3, 2, 2) // 7
const addRecurse = (...nums) => {
// Decided to use if blocks here for readability
if (nums.some(isNaN)) {
return `Sorry all parameters must be numbers`;
}
if (nums.length === 1) {
return nums[0]
} else {
return nums.shift() + addRecurse(...nums);
}
};
// A function (mulRecurse) that is the generalized (mul) function but uses recursion
// Bonus: same again as above
// e.g. mulRecurse(3, 4, 10, "Shwing") // Sorry, all parameters must be numbers
// mulRecurse(3, 1, 9) // 27
const mulRecurse = (...nums) => {
if (nums.some(isNaN)) {
return `Sorry all parameters must be numbers`;
}
if (nums.length === 1) {
return nums[0]
} else {
return nums.shift() * mulRecurse(...nums);
}
};