-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-04.test.js
346 lines (269 loc) · 14.9 KB
/
challenges-04.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named addAnimal that takes in array of animals (strings) and some callback function.
This function should first create a new array. Then iterate over the input array and modify each value based on the callback function provided.
Push each updated animal string into the new array. Return the new array.
HINT: Look at the tests to see how the callback functions are used.
------------------------------------------------------------------------------------------------ */
function upper(str) {
return str.toUpperCase();
}
function lower(str) {
return str.toLowerCase();
}
const updateAnimal = (arr, callback) => arr.map(item => callback(item));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function called sortNames that takes an array of names and sorts them alphabetically. Capital letters should come before lowercase letters.
For example: 'Cat' would come before 'apple'
------------------------------------------------------------------------------------------------ */
const sortNames = arr => arr.sort();
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function called sortNumbers that takes an array of numbers and sorts them from smallest to largest.
HINT: Beware... JS default is "Lexical" ordering.
------------------------------------------------------------------------------------------------ */
const sortNumbers = arr => arr.sort((a,b) => a - b);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named sortBackwards that takes in an array of numbers and returns the same array, with the numbers sorted, largest to smallest.
HINT: Do it with a custom sort callback, not with using `.reverse()`. ;)
------------------------------------------------------------------------------------------------ */
const sortBackwards = arr => arr.sort((a,b) => b - a);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named alphabetize that takes in an array of strings and returns the same array with the strings sorted alphabetically.
In this alphabetization, capital letters come before lower case letters.
For example, ['Alphabet', 'Zebra', 'alphabet', 'carrot'] is correctly sorted.
------------------------------------------------------------------------------------------------ */
const alphabetize = arr => arr.sort();
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named sortByPrice that takes in an array of objects, each of which has a 'price' property, and sorts those objects by price, lowest to highest, returning the same array.
Here is an example of the input:
[
{name: 'Sweatshirt', price: 45},
{name: 'Bookmark', price: 2.50},
{name: 'Tote bag', price: 15}
];
------------------------------------------------------------------------------------------------ */
const sortByPrice = arr => arr.sort((a, b) => +a.price > +b.price ? 1 : -1);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named alphabetizeBetter that takes in an array of strings and returns the same array, with the strings sorted alphabetically. Capitalization should not change the sort order of two strings.
For example, ['Alphabet', 'alphabet', 'carrot', 'Zebra'] is correctly sorted, and so is ['alphabet', 'Alphabet', 'carrot', 'Zebra'].
------------------------------------------------------------------------------------------------ */
// Guide: https://stackoverflow.com/a/45544166/7967484
const alphabetizeBetter = arr => arr.sort((a,b) => a.localeCompare(b));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named sortByLength that takes in an array of strings and returns the same array, with the strings sorted by their length, lowest to highest.
------------------------------------------------------------------------------------------------ */
const sortByLength = arr => arr.sort((a,b) => a.length - b.length);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Write a function named sortNumbersByLength that takes in an array of numbers and sorts those numbers by their length.
For example, [1, 14, 0.2, -281, 54782] is only correctly sorted in that order.
------------------------------------------------------------------------------------------------ */
const sortNumbersByLength = arr => arr.sort((a,b) => a.toString().length - b.toString().length);
/*-----------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named sortPeople that takes in an array of Person objects, each of which has firstName, lastName, and age properties, and sorts those people by their last names. Do not worry about capitalization or first names.
------------------------------------------------------------------------------------------------ */
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
const people = [
new Person('Wes', 'Washington', 25),
new Person('Casey', 'Codefellow', 38),
new Person('Stan', 'Seattle', 67),
];
const sortPeople = arr => arr.sort((a,b) => a.lastName.localeCompare(b.lastName));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 11 - Stretch Goal
Write a function named sortPeopleBetter that takes in an array of Person objects, each of which has firstName, lastName, and age properties, and sorts those people by their last names.
If two people share the same last name, alphabetize on their first name.
If two people have the same full name, the younger one should come first. Do not worry about capitalization.
------------------------------------------------------------------------------------------------ */
const sortPeopleBetter = arr => {
return arr.sort((a,b) => {
try {
if(a.lastName === b.lastName && a.firstName === b.firstName) {
return a.age - b.age;
} else if (a.lastName === b.lastName) {
return a.firstName.localeCompare(b.firstName);
} else {
return a.lastName.localeCompare(b.lastName);
}
} catch(e) {
console.log('Sort Error: ', e);
}
});
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 12 - Stretch Goal
Write a function named sortMeetingsByDay that takes in an array of objects, each of which represents a meeting happening a particular day of the week, with a particular start time and end time.
Sort the meetings by the day on which they happen, Monday-Friday. It does not matter which order meetings come in on a particular day. For example, if there are two meetings on Monday, it does not matter which comes first.
------------------------------------------------------------------------------------------------ */
function Meeting(dayOfWeek, start, end) {
this.dayOfWeek = dayOfWeek;
this.start = start;
this.end = end;
}
const meetings = [
new Meeting('Monday', '0900', '1000'),
new Meeting('Wednesday', '1300', '1500'),
new Meeting('Tuesday', '1145', '1315'),
new Meeting('Wednesday', '0930', '1000'),
new Meeting('Monday', '0900', '0945'),
new Meeting('Friday', '1200', '1345'),
];
const dayOrder = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
const sortMeetingsByDay = arr => arr.sort((a, b) => dayOrder.indexOf(a.dayOfWeek.toLowerCase()) - dayOrder.indexOf(b.dayOfWeek.toLowerCase()));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 13 - Stretch Goal
This challenge should use the array of meetings from challenge 9, above.
Sort the meetings in the order that they start. If two meetings start at the same time on the same day, the shorter meeting should come first.
You DO NOT need to use your solution to Challenge 9 in completing Challenge 10.
------------------------------------------------------------------------------------------------ */
const sortSchedule = arr => arr.sort((a,b) => a.dayOfWeek === b.dayOfWeek ? (a.end - a.start) - (b.end - b.start) : dayOrder.indexOf(a.dayOfWeek.toLowerCase()) - dayOrder.indexOf(b.dayOfWeek.toLowerCase()));
/* ------------------------------------------------------------------------------------------------
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-03.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return an array of uppercase animal names', () => {
const arr = ['BeAr', 'lIon'];
expect(updateAnimal(arr, upper)[0]).toStrictEqual('BEAR');
expect(updateAnimal(arr, upper)[1]).toStrictEqual('LION');
});
test('It should return an array of lowercase animal names', () => {
const arr = ['BeAr', 'lIon'];
expect(updateAnimal(arr, lower)[0]).toStrictEqual('bear');
expect(updateAnimal(arr, lower)[1]).toStrictEqual('lion');
});
});
describe('Testing challenge 2', () => {
test('It should return an array of names sorted alphabetically', () => {
expect(sortNames(['able', 'Bob'])[0]).toStrictEqual('Bob');
});
});
describe('Testing challenge 3', () => {
test('It should sort low-to-high the numbers in an array', () => {
expect(sortNumbers([8, 3, 2, 9, 12, 1, 115])).toStrictEqual([1, 2, 3, 8, 9, 12, 115]);
});
});
describe('Testing challenge 4', () => {
test('It should sort high-to-low the numbers in an array', () => {
const nums = [3,4,5,6,7];
expect(sortBackwards(nums)).toStrictEqual([7,6,5,4,3]);
expect(sortBackwards([3,2,1])).toStrictEqual([3,2,1]);
expect(sortBackwards([12,20,3])).toStrictEqual([20, 12, 3]);
expect(sortBackwards([])).toStrictEqual([]);
expect(sortBackwards([1])).toStrictEqual([1]);
});
});
describe('Testing challenge 5', () => {
test('It should sort strings alphabetically', () => {
expect(alphabetize(['alphabet', 'Zebra', 'Alphabet', 'carrot'])).toStrictEqual([ 'Alphabet', 'Zebra', 'alphabet', 'carrot']);
expect(alphabetize(['alphabet','Alphabet', 'carrot'])).toStrictEqual([ 'Alphabet', 'alphabet', 'carrot']);
expect(alphabetize([])).toStrictEqual([]);
});
});
describe('Testing challenge 6', () => {
test('It should sort items by their price', () => {
expect(sortByPrice([
{name: 'Sweatshirt', price: 45},
{name: 'Bookmark', price: 2.50},
{name: 'Tote bag', price: 15}
])).toStrictEqual([
{name: 'Bookmark', price: 2.50},
{name: 'Tote bag', price: 15},
{name: 'Sweatshirt', price: 45},
]);
expect(sortByPrice([{price: 12}, {price: 10}])).toStrictEqual([{price: 10}, {price: 12}]);
expect(sortByPrice([])).toStrictEqual([]);
});
});
describe('Testing challenge 7', () => {
test('It should sort strings by length', () => {
const ans = sortByLength(['alphabet', 'Zebra', 'Alphabet', 'carrot']);
expect(ans.slice(0,2)).toStrictEqual(['Zebra', 'carrot']);
expect(ans.slice(2,4)).toEqual(expect.arrayContaining(['Alphabet', 'alphabet']));
expect(sortByLength(['a', 'bc', ''])).toStrictEqual(['', 'a', 'bc']);
expect(sortByLength(['a'])).toStrictEqual(['a']);
expect(sortByLength([])).toStrictEqual([]);
});
});
describe('Testing challenge 8', () => {
test('It should alphabetize without regard to capitalization', () => {
expect(alphabetizeBetter(['Alice', 'apple', 'alert', 'Average'])).toStrictEqual([ 'alert', 'Alice', 'apple', 'Average' ]);
const ans = alphabetizeBetter(['alphabet', 'Zebra', 'Alphabet', 'carrot']);
expect(ans.slice(0,2)).toEqual(expect.arrayContaining([ 'Alphabet','alphabet']));
expect(ans.slice(2)).toStrictEqual(['carrot', 'Zebra']);
});
});
describe('Testing challenge 9', () => {
test('It should sort numbers by their length', () => {
expect(sortNumbersByLength([10, 2.8, 1, -47.75])).toStrictEqual([1, 10, 2.8, -47.75]);
expect(sortNumbersByLength([100, 2.82, 1, -47.75])).toStrictEqual([1, 100, 2.82, -47.75]);
expect(sortNumbersByLength([1,2,3])).toEqual(expect.arrayContaining([1,2,3]));
});
});
describe('Testing challenge 10', () => {
test('It should sort people by their last names', () => {
expect(sortPeople(people)).toStrictEqual([
new Person('Casey', 'Codefellow', 38),
new Person('Stan', 'Seattle', 67),
new Person('Wes', 'Washington', 25),
]);
expect(sortPeople([{lastName: 'banana'}, {lastName: 'apple'}]))
.toStrictEqual([{lastName: 'apple'}, {lastName: 'banana'}]);
});
});
describe('Testing challenge 11', () => {
test('It should sort people with more strict ordering', () => {
const family = [
new Person('Casey', 'Codefellows', 55),
new Person('Casey', 'Codefellows', 37),
new Person('Charlie', 'Codefellows', 21),
new Person('Charles', 'Codefellows', 29),
new Person('Carol', 'Codefellow', 88),
];
expect(sortPeopleBetter(family)).toStrictEqual([
new Person('Carol', 'Codefellow', 88),
new Person('Casey', 'Codefellows', 37),
new Person('Casey', 'Codefellows', 55),
new Person('Charles', 'Codefellows', 29),
new Person('Charlie', 'Codefellows', 21),
]);
expect(sortPeopleBetter([{firstName: 'andrew', lastName: 'apple'}, {firstName: 'andre', lastName: 'apple'}]))
.toStrictEqual([{firstName: 'andre', lastName: 'apple'}, {firstName: 'andrew', lastName: 'apple'}]);
});
});
describe('Testing challenge 12', () => {
test('It should sort meetings by the day on which they happen', () => {
const sortedMeetings = sortMeetingsByDay(meetings);
expect(sortedMeetings.slice(0,2)).toEqual(expect.arrayContaining([new Meeting('Monday', '0900', '0945'), new Meeting('Monday', '0900', '1000')]));
expect(sortedMeetings[2]).toStrictEqual(new Meeting('Tuesday', '1145', '1315'));
expect(sortedMeetings.slice(3,5)).toEqual(expect.arrayContaining([new Meeting('Wednesday', '0930', '1000'), new Meeting('Wednesday', '1300', '1500')]));
expect(sortedMeetings[5]).toStrictEqual(new Meeting('Friday', '1200', '1345'));
});
});
describe('Testing challenge 13', () => {
test('It should sort meetings by when they happen', () => {
expect(sortSchedule(meetings)).toStrictEqual([
new Meeting('Monday', '0900', '0945'),
new Meeting('Monday', '0900', '1000'),
new Meeting('Tuesday', '1145', '1315'),
new Meeting('Wednesday', '0930', '1000'),
new Meeting('Wednesday', '1300', '1500'),
new Meeting('Friday', '1200', '1345'),
]);
});
});