-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-07.test.js
382 lines (300 loc) · 14.5 KB
/
challenges-07.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function called sortStarWarsCharacters that sorts the characters in the starWarsPeople array by height from tallest to shortest.
------------------------------------------------------------------------------------------------ */
let starWarsPeople = [
{
"name": "C-3PO",
"height": "167",
"eye_color": "yellow"
},
{
"name": "Luke Skywalker",
"height": "172",
"eye_color": "blue"
},
{
"name": "R2-D2",
"height": "96",
"eye_color": "red"
}
];
// Guide: https://flaviocopes.com/how-to-sort-array-of-objects-by-property-javascript
const sortStarWarsCharacters = (starWarsArr) => starWarsArr.sort((a,b) => (+a.height < +b.height) ? 1 : -1);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named removeThree that takes an index and an array. The function should removes three items in the array starting with the value at the index.
------------------------------------------------------------------------------------------------ */
const removeThree = (idx, arr) => {
arr.splice(idx, 3);
return arr;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named joinArray that takes an array and joins all of the elements together in one string on a space.
------------------------------------------------------------------------------------------------ */
const joinArray = (arr) => {
return arr.join(' ');
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named howMuchPencil that takes in a string, as written on the side of a pencil.
As you sharpen the pencil, the string will become shorter and shorter, starting by removing the first letter.
Your function should use slice within a loop and return an array of each successive string result from losing letters to the sharpener, until nothing is left.
For example, if the input is 'Welcome', the output will be:
['Welcome', 'elcome', 'lcome', 'come', 'ome', 'me', 'e', ''].
------------------------------------------------------------------------------------------------ */
const howMuchPencil = (str) => {
let result = [];
for( let i = 0; i < str.length + 1; i++ ) {
result.push(str.slice(i));
}
return result;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function name wordsToCharList that, given a string as input, returns a new array where every element is a character of the input string.
For example, wordsToCharList('gregor') returns ['g','r','e','g','o','r'].
------------------------------------------------------------------------------------------------ */
const wordsToCharList = (arr) => {
let result = [];
for( let i = 0; i < arr.length; i++) {
result.push(arr[i]);
}
return result;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
You are making a grocery list for ingredients needed in the gruffalo crumble recipe, below. Rather than taking the entire recipe, you only want a list of the item names.
Write a function named listFoods that takes in the recipe and returns a new array of the food items without any amount or units. Just the name. For example, '1 cup flour' will return 'flour'.
Use slice for this function, maybe more than once. The Array.indexOf() method may also be helpful.
Do not use split for this function.
------------------------------------------------------------------------------------------------ */
const gruffaloCrumble = {
name: 'How to make a Gruffalo Crumble',
ingredients: [
'1 medium-sized Gruffalo',
'8 pounds oats',
'2 pounds brown sugar',
'4 pounds flour',
'2 gallons pure maple syrup',
'16 cups chopped nuts',
'1 pound baking soda',
'1 pound baking powder',
'1 pound cinnamon',
'6 gallons melted butter',
'2 gallons fresh water',
],
steps: [
'Pre-heat a large oven to 375',
'De-prickle the gruffalo',
'Sprinkle with cinnamon, sugar, flour, and nuts',
'Mix until evenly distributed',
'Grease a 3-foot x 3-foot casserole dish',
'Combine gruffalo compote with water to maintain moisture in the oven',
'Fold together remaining ingredients to make the crisp',
'Spread the crisp evenly over the gruffalo mixture',
'Bake for 12-15 hours',
]
};
const listFoods = (recipe) => {
let result = [];
recipe.ingredients.forEach(entry => {
let firstEntry = entry.indexOf(' ');
result.push(entry.slice(entry.indexOf(' ', firstEntry + 1), entry.length).trim());
});
return result;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named splitFoods that uses split to produce the same output as Challenge 3.
You may also use other string or array methods.
------------------------------------------------------------------------------------------------ */
const splitFoods = (recipe) => {
let result = [];
recipe.ingredients.forEach(entry => {
result.push(entry.split(' ').slice(2).join(' '));
});
return result;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Use the same recipe from Challenge 3, above.
Write a function named stepAction that takes in the recipe and extracts the action verbs from the steps. Fortunate for you, the action verbs are the first word of each action.
Return a new array containing just the verbs. For example, ['Mix until evenly distributed'] returns ['Mix'].
------------------------------------------------------------------------------------------------ */
const stepActions = (recipe) => {
let result = [];
recipe.steps.forEach(entry => {
result.push(entry.split(' ')[0]);
});
return result;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Write a function named removeEvenValues that, given an array of integers as input, deletes all even values from the array, leaving no 'gaps' behind.
The array should be modified in-place.
For example:
const integers = [1, 2, 3, 4, 5, 6];
removeEvenValues(integers);
console.log(integers) will print [1, 3, 5]
------------------------------------------------------------------------------------------------ */
const removeEvenValues = (arr) => {
for (var i = 0; i < arr.length; i++) {
while (arr[i] % 2 === 0) {
arr.splice(i, 1);
}
}
return arr;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named removeLastCharacters that takes in a string and a number. The numberOfCharacters argument determines how many characters will be removed from the end of the string. Return the resulting string.
If the numberOfCharacters argument is greater than the length of the input string, the function should return an empty string.
If the numberOfCharacters argument input is a negative number, the function should return the input string without any changes.
For example:
removeLastCharacters('Gregor', 2) returns 'Greg'
removeLastCharacters('Gregor', -2) returns 'Gregor'
removeLastCharacters('Gregor', 9) returns ''
------------------------------------------------------------------------------------------------ */
const removeLastCharacters = (str, numberOfCharacters) => {
if(numberOfCharacters < 0) {
return str;
} else if (str < numberOfCharacters) {
return '';
} else {
return str.slice(0, -numberOfCharacters);
}
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 11 - Stretch Goal
Write a function named totalSumCSV that, given a string of comma-separated values (CSV) as input. (e.g. "1,2,3"), returns the total sum of the numeric values (e.g. 6).
------------------------------------------------------------------------------------------------ */
const totalSumCSV = (str) => {
let total = 0;
let arr = str.split(',');
arr.forEach(item => {
total += +item;
});
return total;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 12 - Stretch Goal
Write a function named removeVowels that takes in a string and returns a new string where all the vowels of the original string have been removed.
For example, removeVowels('gregor') returns 'grgr'.
------------------------------------------------------------------------------------------------ */
const removeVowels = (str) => str.replace(/[aeiou]/ig,'');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 13 - Stretch Goal
Write a function named extractVowels that takes in a string and returns an array where the first element is the original string with all the vowels removed, and the second element is a string of all the vowels that were removed, in alphabetical order.
For example, extractVowels('gregor') returns ['grgr', 'eo'].
Similarly, extractVowels('The quick brown fox') returns ['Th qck brwn fx', 'eioou']
------------------------------------------------------------------------------------------------ */
const extractVowels = (str) => {
let vowelLess = str.replace(/[aeiou]/ig,'');
let vowelOrdered = str.replace(/[^aeiou]/ig,'').split('').sort().join('');
return [vowelLess, vowelOrdered];
};
/* ------------------------------------------------------------------------------------------------
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-05.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should sort the star wars characters by height from tallest to shortest', () => {
expect(sortStarWarsCharacters(starWarsPeople)[0]['name']).toStrictEqual('Luke Skywalker');
expect(sortStarWarsCharacters(starWarsPeople)[2]['height']).toStrictEqual('96');
});
});
describe('Testing challenge 2', () => {
test('It should return an array with three items removed', () => {
expect(removeThree(2, [1, 2, 3, 4, 5, 6, 7, 8])).toStrictEqual([1, 2, 6, 7, 8]);
});
});
describe('Testing challenge 3', () => {
test('It should join an array', () => {
expect(joinArray(['hello', '301', 'students'])).toStrictEqual('hello 301 students');
});
});
describe('Testing challenge 4', () => {
test('It should return a list of shortening words', () => {
expect(howMuchPencil('Welcome')).toStrictEqual(['Welcome', 'elcome', 'lcome', 'come', 'ome', 'me', 'e', '']);
expect(howMuchPencil('Welcome').length).toStrictEqual(8);
expect(howMuchPencil('')).toStrictEqual(['']);
expect(howMuchPencil('abc')).toStrictEqual(['abc', 'bc', 'c', '']);
});
});
describe('Testing challenge 5', () => {
test('It should return an array of individual letters', () => {
expect(wordsToCharList('Gregor')).toStrictEqual(['G', 'r', 'e', 'g', 'o', 'r']);
expect(wordsToCharList('Gregor').length).toStrictEqual(6);
expect(wordsToCharList('hooray')).toStrictEqual(['h', 'o', 'o', 'r', 'a', 'y']);
expect(wordsToCharList('')).toStrictEqual([]);
});
});
describe('Testing challenge 6', () => {
test('It should return a list of foods', () => {
expect(listFoods(gruffaloCrumble)).toStrictEqual(['Gruffalo', 'oats', 'brown sugar', 'flour', 'pure maple syrup', 'chopped nuts', 'baking soda', 'baking powder', 'cinnamon', 'melted butter', 'fresh water']);
expect(listFoods(gruffaloCrumble).length).toStrictEqual(11);
});
});
describe('Testing challenge 7', () => {
test('It should return a list of foods', () => {
expect(splitFoods(gruffaloCrumble)).toStrictEqual(['Gruffalo', 'oats', 'brown sugar', 'flour', 'pure maple syrup', 'chopped nuts', 'baking soda', 'baking powder', 'cinnamon', 'melted butter', 'fresh water']);
});
});
describe('Testing challenge 8', () => {
test('It should return a list of recipe steps', () => {
expect(stepActions(gruffaloCrumble)).toStrictEqual(['Pre-heat', 'De-prickle', 'Sprinkle', 'Mix', 'Grease', 'Combine', 'Fold', 'Spread', 'Bake']);
expect(stepActions(gruffaloCrumble).length).toStrictEqual(9);
});
});
describe('Testing challenge 9', () => {
test('It should remove the even numbers from the array', () => {
let list = [1, 2, 3, 4, 5, 6];
removeEvenValues(list);
expect(list).toStrictEqual([1, 3, 5]);
list = [6, 3, 19, 43, 12, 66, 43];
removeEvenValues(list);
expect(list).toStrictEqual([3, 19, 43, 43]);
expect(list.length).toStrictEqual(4);
});
});
describe('Testing challenge 10', () => {
test('It should shorten the string based on the first argument', () => {
expect(removeLastCharacters('Gregor', 2)).toStrictEqual('Greg');
expect(removeLastCharacters('Gregor', 2).length).toStrictEqual(4);
});
test('It should return the complete string when passed a negative number', () => {
expect(removeLastCharacters('hello', -1)).toStrictEqual('hello');
expect(removeLastCharacters('wowow', -700)).toStrictEqual('wowow');
});
test('It should return an empty string when called with a number larger than the string length', () => {
expect(removeLastCharacters('hello', 12)).toStrictEqual('');
expect(removeLastCharacters('', 1)).toStrictEqual('');
expect(removeLastCharacters('a', 1)).toStrictEqual('');
});
});
describe('Testing challenge 11', () => {
test('It should add up the numbers contained within the string', () => {
expect(totalSumCSV('1,4,5,7,2')).toStrictEqual(19);
expect(totalSumCSV('147')).toStrictEqual(147);
});
});
describe('Testing challenge 12', () => {
test('It should return the string without vowels', () => {
expect(removeVowels('gregor')).toStrictEqual('grgr');
expect(removeVowels('gregor').length).toStrictEqual(4);
expect(removeVowels('asdf')).toStrictEqual('sdf');
expect(removeVowels('why')).toStrictEqual('why');
});
});
describe('Testing challenge 13', () => {
test('It should return the string without vowels', () => {
expect(extractVowels('gregor')).toStrictEqual(['grgr', 'eo']);
expect(extractVowels('gregor').length).toStrictEqual(2);
expect(extractVowels('The quick brown fox')).toStrictEqual(['Th qck brwn fx', 'eioou']);
});
});