-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-03.test.js
305 lines (241 loc) · 12.6 KB
/
challenges-03.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
296
297
298
299
300
301
302
303
304
305
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function called addTwo that takes in an array and adds two to every value using a for loop. Place the new value in a new array. Return the new array.
------------------------------------------------------------------------------------------------ */
const addTwo = (arr) => arr.map(item => item + 2);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named typeNum that, given an array as input, uses filter to return an array containing only the numbers.
For example, typeNum([1, 'bob' ,3]) returns [1,3].
------------------------------------------------------------------------------------------------ */
const typeNum = (arr) => arr.filter(item => typeof(item) === 'number');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named containsAnd that, given an array of strings as input, uses filter to return an array containing only strings that contain 'and' within the string.
For example, containsAnd(['panda', 'ran', 'and']) returns ['panda', 'and'].
------------------------------------------------------------------------------------------------ */
const containsAnd = (arr) => arr.filter(item => item.match(/and/ig));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named oddValues that, given an array of integers as input, uses filter to return an array containing only the odd integers.
For example, oddValues([1,2,3]) returns [1,3].
------------------------------------------------------------------------------------------------ */
const oddValues = (arr) => arr.filter(item => item % 2 !== 0);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named filterStringsWithVowels that, given an array of strings as input, uses filter to return an array with only words that contain vowels.
The callback function to filter should include or utilize a regular expression pattern.
For example, filterStringsWithVowels('gregor','hound','xyz') returns ['gregor', 'hound'].
------------------------------------------------------------------------------------------------ */
const filterStringsWithVowels = (arr) => arr.filter(item => item.match(/[aeiou]/ig));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named notInFirstArray that, given two arrays as input, uses filter to return an array of all the elements in the second array that are not included in the first array.
For example, notInFirstArray([1,2,3], [1,2,3,4]) returns [4].
------------------------------------------------------------------------------------------------ */
const notInFirstArray = (forbiddenValues, arr) => arr.filter(item => !forbiddenValues.includes(item));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named getBaseStatGreaterThan that, given the snorlaxData, below, and an integer as input, uses filter to return an array containing all stats with a baseStat greater than the integer.
For example, getBaseStatGreaterThan(snorlaxData.stats, 50) will return an array containing the 'special-defense' and 'special-attack' objects.
------------------------------------------------------------------------------------------------ */
const snorlaxData = {
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 getBaseStatGreaterThan = (arr, minBaseStat) => arr.filter(item => item.baseStat > minBaseStat);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named getStatName that is an extension of your getBaseStatGreaterThan function from challenge 4. For this function, extend your solution from challenge 4 to only return the name of the stat, rather than the entire stat object.
For example, getStatName(snorlaxData.stats, 50) will return ['special-defense', 'special-attack'].
------------------------------------------------------------------------------------------------ */
const getStatName = (arr, minBaseStat) => arr.filter(item => item.baseStat > minBaseStat).map(item => item.stat.name);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Write a function named getCharactersWithoutChildren that, given the array of characters, below, uses filter to return an array of all characters without children.
------------------------------------------------------------------------------------------------ */
const characters = [
{
name: 'Eddard',
spouse: 'Catelyn',
children: ['Robb', 'Sansa', 'Arya', 'Bran', 'Rickon'],
house: 'Stark',
},
{
name: 'Jon',
spouse: 'Lysa',
children: ['Robin'],
house: 'Arryn',
},
{
name: 'Cersei',
spouse: 'Robert',
children: ['Joffrey', 'Myrcella', 'Tommen'],
house: 'Lannister',
},
{
name: 'Daenarys',
spouse: 'Khal Drogo',
children: ['Drogon', 'Rhaegal', 'Viserion'],
house: 'Targaryen',
},
{
name: 'Mace',
spouse: 'Alerie',
children: ['Margaery', 'Loras'],
house: 'Tyrell',
},
{
name: 'Sansa',
spouse: 'Tyrion',
house: 'Stark',
},
{
name: 'Jon',
spouse: null,
house: 'Snow',
},
];
const getCharactersWithoutChildren = (arr) => arr.filter(item => !item.children);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named evenOddNumericValues that, given an array as input, uses filter to remove any non-numeric values, then uses map to generate a new array containing the string 'even' or 'odd', depending on the original value.
For example: evenOddNumericValues(['Gregor', 2, 4, 1]) returns ['even', 'even', 'odd'].
------------------------------------------------------------------------------------------------ */
const evenOddNumericValues = (arr) => arr.filter(item => typeof(item) === 'number').map(item => item % 2 === 0 ? 'even' : 'odd');
/* ------------------------------------------------------------------------------------------------
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-08.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should add two to every value', () => {
expect(addTwo([1, 2, 4])).toStrictEqual([3, 4, 6]);
});
});
describe('Testing challenge 2', () => {
test('It should return an array containing only numbers', () => {
expect(typeNum([1, 'bob', 3])).toStrictEqual([1, 3]);
expect(typeNum([1, 'bob', 3]).length).toStrictEqual(2);
expect(typeNum(['banana', 'apples', 'cherry'])).toStrictEqual([]);
expect(typeNum([2, 3, 5])).toStrictEqual([2, 3, 5]);
});
});
describe('Testing challenge 3', () => {
test('It should return an array of strings containing the word and', () => {
expect(containsAnd(['panda', 'ran', 'and'])).toStrictEqual(['panda', 'and']);
expect(containsAnd(['banana','bob','xyz'])).toStrictEqual([]);
expect(containsAnd([])).toStrictEqual([]);
expect(containsAnd(['and', 'sand'])).toStrictEqual(['and', 'sand']);
});
});
describe('Testing challenge 4', () => {
test('It should return an array containing only odd integers', () => {
expect(oddValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toStrictEqual([1, 3, 5, 7, 9]);
expect(oddValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).length).toStrictEqual(5);
expect(oddValues([2,3,4,179])).toStrictEqual([3,179]);
expect(oddValues([2,4,6,8])).toStrictEqual([]);
});
});
describe('Testing challenge 5', () => {
test('It should return an array containing only words that have vowels', () => {
expect(filterStringsWithVowels(['gregor','hound','xyz'])).toStrictEqual(['gregor', 'hound']);
expect(filterStringsWithVowels(['gregor','hound','xyz']).length).toStrictEqual(2);
expect(filterStringsWithVowels(['a', 'b', 'cdefg'])).toStrictEqual(['a', 'cdefg']);
expect(filterStringsWithVowels(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ''])).toStrictEqual(['a', 'e', 'i', 'o', 'u']);
});
test('It should not contain any words that do not contain vowels', () => {
expect(filterStringsWithVowels(['gregor','hound','xyz'])).not.toContain('xyz');
});
});
describe('Testing challenge 6', () => {
const firstNums = [1, 2, 3];
const secondNums = [1, 2, 3, 4];
const firstStrings = ['Demi', 'Gregor', 'Hound'];
const secondStrings = ['Gary', 'Charlotte', 'Demi', 'Gregor', 'Hound'];
test('It should return an array that includes any elements not in the first array', () => {
expect(notInFirstArray(firstNums, secondNums)).toStrictEqual([4]);
expect(notInFirstArray(firstNums, secondNums).length).toStrictEqual(1);
});
test('It should also work with an array of strings', () => {
expect(notInFirstArray(firstStrings, secondStrings)).toStrictEqual(['Gary', 'Charlotte']);
expect(notInFirstArray(firstStrings, secondStrings).length).toStrictEqual(2);
});
test('It should work with empty arrays', () => {
expect(notInFirstArray([], [])).toStrictEqual([]);
expect(notInFirstArray([], [1,2,3,4,5])).toStrictEqual([1,2,3,4,5]);
expect(notInFirstArray([1,2,3,4,5], [])).toStrictEqual([]);
});
});
describe('Testing challenge 7', () => {
test('It should return an array containing the stats that are greater than the input', () => {
expect(getBaseStatGreaterThan(snorlaxData.stats, 75)).toStrictEqual([ { stat: { url: 'https://pokeapi.co/api/v2/stat/5/', name: 'special-defense' }, effort: 2, baseStat: 110 } ]);
expect(getBaseStatGreaterThan(snorlaxData.stats, 75).length).toStrictEqual(1);
expect(getBaseStatGreaterThan(snorlaxData.stats, 110)).toStrictEqual([]);
});
test('It should work for non-Snorlax data', () => {
expect(getBaseStatGreaterThan([{baseStat: 10}, {baseStat: -85}, {baseStat: 0}, {baseStat: -50}], -60)).toStrictEqual([{baseStat: 10}, {baseStat: 0}, {baseStat: -50}]);
});
});
describe('Testing challenge 8', () => {
test('It should return the name of the stats that exceed that maximum', () => {
expect(getStatName(snorlaxData.stats, 50)).toStrictEqual([ 'special-defense', 'special-attack' ]);
expect(getStatName(snorlaxData.stats, 50).length).toStrictEqual(2);
});
test('It should return the name of the stats that exceed that maximum', () => {
expect(getStatName(snorlaxData.stats, 120)).toStrictEqual([]);
expect(getStatName(snorlaxData.stats, 120).length).toStrictEqual(0);
});
test('It should work for non-snorlax data', () => {
expect(getStatName([
{baseStat: 10, stat: {name: 'one'}},
{baseStat: -85, stat: {name: 'two'}},
{baseStat: 0, stat: {name: 'three'}},
{baseStat: -50, stat: {name: 'four'}}
], -60)).toStrictEqual(['one', 'three', 'four']);
});
});
describe('Testing challenge 9', () => {
test('It should return an array containing characters who do not have children', () => {
expect(getCharactersWithoutChildren(characters)).toStrictEqual([ { name: 'Sansa', spouse: 'Tyrion', house: 'Stark' }, { name: 'Jon', spouse: null, house: 'Snow' } ]);
expect(getCharactersWithoutChildren(characters).length).toStrictEqual(2);
});
});
describe('Testing challenge 10', () => {
test('It should remove non-integers and return "even" or "odd', () => {
expect(evenOddNumericValues(['Gregor', 2, 4, 1])).toStrictEqual(['even', 'even', 'odd']);
expect(evenOddNumericValues(['Gregor', 2, 4, 1]).length).toStrictEqual(3);
expect(evenOddNumericValues(['a', 'b', 'c'])).toStrictEqual([]);
});
test('It should not accept strings that look like numbers', () => {
expect(evenOddNumericValues(['1', 2, 3, '4', 5,'6'])).toStrictEqual(['even', 'odd', 'odd']);
});
});