-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-02.test.js
295 lines (229 loc) · 11.1 KB
/
challenges-02.test.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named raisedToTheThird that takes in an array of numbers and returns a new array of each of those numbers raised to the 3rd power (hint: look up Math.pow()). Use forEach to solve this problem.
------------------------------------------------------------------------------------------------ */
const raisedToTheThird = (arr) => {
let returnArray = [];
arr.forEach(item => {
returnArray.push(Math.pow(item, 3));
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named addOne that, given an array of numbers, uses map to return a new array with each value simply incremented by 1.
------------------------------------------------------------------------------------------------ */
const addOne = (arr) => arr.map(item => item +1);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named addQuestion that, given an array of strings, uses map to return a new array containing each string followed by a question mark character.
------------------------------------------------------------------------------------------------ */
const addQuestion = (arr) => arr.map(item => `${item}?`);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named forLoopTwoToThe that, given an array of integers as input, iterates over the array and returns a new array. The returned array should contain the result of raising 2 to the power of the original input element.
You may choose to complete this challenge using a for loop, for...in syntax, or for...of syntax.
For example, twoToThe([1,2,3]) returns [2,4,8] because 2 ^ 1 = 2, 2 ^ 2 = 4, and 2 ^ 3 = 8.
------------------------------------------------------------------------------------------------ */
const forLoopTwoToThe = (arr) => {
const returnArray = [];
for (let i = 0; i < arr.length; i++) {
returnArray.push(Math.pow(2, arr[i]));
}
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named forEachTwoToThe that produces the same output as your forLoopTwoToThe function from challenge 1, but uses forEach instead of a for loop.
------------------------------------------------------------------------------------------------ */
const forEachTwoToThe = (arr) => {
const returnArray = [];
arr.forEach(item => {
returnArray.push(Math.pow(2, item));
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named mapTwoToThe that produces the same output as your forLoopTwoToThe function from challenge 1 and your forEachTwoToThe function from challenge 2, but uses map instead of a for loop or forEach.
------------------------------------------------------------------------------------------------ */
const mapTwoToThe = (arr) => arr.map(item => Math.pow(2, item));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named charCode that, given an array of letters as an input, uses map to return a new array where each element is the result of the `charCodeAt` method on the original array element.
Read the MDN documentation on String.charCodeAt() if necessary.
For example: charCode(['h','i']) returns [104, 105].
------------------------------------------------------------------------------------------------ */
const charCode = (arr) => arr.map(item => item.charCodeAt());
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function that, given an array of numbers as input, uses map to return a new array where each element is either the string "even" or the string "odd", based on each value.
If any element in the array is not a number, the resulting array should have the string "N/A" in its place.
For example: evenOdd([1,2,3]) returns ['odd','even','odd'].
------------------------------------------------------------------------------------------------ */
const evenOdd = (arr) => arr.map(item => typeof(item) !== 'number' ? 'N/A': item % 2 === 0 ? 'even' : 'odd');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Use the snorlaxAbilities data, below, for this challenge.
Write a function named extractAbilities that, given the array of abilities, uses map to create an array containing only the ability name.
Note: Because this function is expecting the array of abilities, it will be invoked as:
extractAbilities(snorlaxAbilities.abilities)
------------------------------------------------------------------------------------------------ */
const snorlaxAbilities = {
abilities: [
{
slot: 3,
is_hidden: true,
ability: {
url: 'https://pokeapi.co/api/v2/ability/82/',
name: 'gluttony',
},
},
{
slot: 2,
is_hidden: false,
ability: {
url: 'https://pokeapi.co/api/v2/ability/56/',
name: 'cute charm',
},
},
{
slot: 1,
is_hidden: false,
ability: {
url: 'https://pokeapi.co/api/v2/ability/17/',
name: 'immunity',
},
},
],
name: 'snorlax',
weight: 4600,
};
const extractAbilities = (arr) => arr.map(entry => entry.ability.name);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Use the snorlaxStats data, below, for this challenge.
Write a function named extractStats that, given an array of stats, uses map to return an array of objects containing the stat name and the total.
The total should be the sum of the effort and the baseStat.
Here is an example of a single array element: { name: 'speed', total: 35 }
------------------------------------------------------------------------------------------------ */
const snorlaxStats = {
stats: [
{
stat: {
url: 'https://pokeapi.co/api/v2/stat/6/',
name: 'speed',
},
effort: 5,
baseStat: 30,
},
{
stat: {
url: 'https://pokeapi.co/api/v2/stat/5/',
name: 'special-defense',
},
effort: 2,
baseStat: 110,
},
{
stat: {
url: 'https://pokeapi.co/api/v2/stat/4/',
name: 'special-attack',
},
effort: 9,
baseStat: 65,
},
],
name: 'snorlax',
weight: 4600,
};
const extractStats = (arr) => arr.map(entry => ({name: entry.stat.name, total: entry.effort + entry.baseStat}));
/* ------------------------------------------------------------------------------------------------
TESTS
All the code below will verify that your functions are working to solve the challenges.
DO NOT CHANGE any of the below code.
Run your tests from the console: jest challenges-07.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return a new array of numbers raised to the thrid power', () => {
expect(raisedToTheThird([2, 4, 5, -7, 0])).toStrictEqual([8, 64, 125, -343, 0]);
});
});
describe('Testing challenge 2', () => {
test('It should add one to all the numbers in the array', () => {
expect(addOne([2, 3, 5, 11])).toStrictEqual([3, 4, 6, 12]);
});
});
describe('Testing challenge 3', () => {
test('It should add a question mark to the end of each string', () => {
expect(addQuestion(['hello', '301', 'students'])).toStrictEqual(['hello?', '301?', 'students?']);
});
});
describe('Testing challenge 4', () => {
test('It should return two raised to the power of the integer', () => {
expect(forLoopTwoToThe([0, 4, 5])).toStrictEqual([1, 16, 32]);
expect(forLoopTwoToThe([0, 4, 5]).length).toStrictEqual(3);
});
test('It should return decimals if the integer is negative', () => {
expect(forLoopTwoToThe([-1, -2, -3])).toStrictEqual([0.5, 0.25, 0.125]);
});
});
describe('Testing challenge 5', () => {
test('It should return two raised to the power of the integer', () => {
expect(forEachTwoToThe([0, 4, 5])).toStrictEqual([1, 16, 32]);
expect(forEachTwoToThe([0, 4, 5]).length).toStrictEqual(3);
});
test('It should return decimals if the integer is negative', () => {
expect(forEachTwoToThe([-1, -2, -3])).toStrictEqual([0.5, 0.25, 0.125]);
});
});
describe('Testing challenge 6', () => {
test('It should return two raised to the power of the integer', () => {
expect(mapTwoToThe([0, 4, 5])).toStrictEqual([1, 16, 32]);
expect(mapTwoToThe([0, 4, 5]).length).toStrictEqual(3);
});
test('It should return decimals if the integer is negative', () => {
expect(mapTwoToThe([-1, -2, -3])).toStrictEqual([0.5, 0.25, 0.125]);
});
});
describe('Testing challenge 7', () => {
test('It should return an array containing the character code for each letter', () => {
expect(charCode(['C', 'o', 'd', 'e', '3', '0', '1'])).toStrictEqual([ 67, 111, 100, 101, 51, 48, 49 ]);
expect(charCode(['C', 'o', 'd', 'e', '3', '0', '1']).length).toStrictEqual(7);
});
});
describe('Testing challenge 8', () => {
test('It should return an array containing the keys from an object', () => {
expect(evenOdd([5, 8, 2, 6, 9, 13, 542, 541])).toStrictEqual([ 'odd', 'even', 'even', 'even', 'odd', 'odd', 'even', 'odd' ]);
expect(evenOdd([5, 8, 2, 6, 9, 13, 542, 541]).length).toStrictEqual(8);
});
test('It should work with all odd numbers', () => {
expect(evenOdd([1, 3, 5, 7, 9])).toStrictEqual([ 'odd', 'odd', 'odd', 'odd', 'odd' ]);
expect(evenOdd([1, 3, 5, 7, 9]).length).toStrictEqual(5);
});
test('It should work with all even numbers', () => {
expect(evenOdd([2, 4, 6, 8, 10])).toStrictEqual([ 'even', 'even', 'even', 'even', 'even' ]);
expect(evenOdd([2, 4, 6, 8, 10]).length).toStrictEqual(5);
});
test('It should return the string "N/A" if a non-number is included in the array', () => {
expect(evenOdd([5, 8, 2, 'hi'])).toStrictEqual([ 'odd', 'even', 'even', 'N/A' ]);
expect(evenOdd([5, 8, 2, 'hi']).length).toStrictEqual(4);
});
});
describe('Testing challenge 9', () => {
test('It should return an array containing only the ability names', () => {
expect(extractAbilities(snorlaxAbilities.abilities)).toStrictEqual(['gluttony', 'cute charm', 'immunity']);
expect(extractAbilities(snorlaxAbilities.abilities).length).toStrictEqual(3);
});
});
describe('Testing challenge 10', () => {
test('It should return an array containing objects with name and total values', () => {
expect(extractStats(snorlaxStats.stats)).toStrictEqual([
{ name: 'speed', total: 35, },
{ name: 'special-defense', total: 112, },
{ name: 'special-attack', total: 74, },
]);
expect(extractStats(snorlaxStats.stats).length).toStrictEqual(3);
});
});