-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-13.test.js
290 lines (220 loc) · 12.9 KB
/
challenges-13.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named longestString that takes in an array of strings and returns the index position of the longest string.
------------------------------------------------------------------------------------------------ */
const longestString = (arr) => arr.length ? arr.indexOf(arr.reduce((a,b) => a.length > b.length ? a : b)) : -1;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named firstLetters that takes in an array of strings and returns an array containing only the first letter of each string.
For example, ['this is great :)', 'wow', 'whyyyyyy :(', ':)))))'] returns ['t', 'w', 'w', ':']
------------------------------------------------------------------------------------------------ */
const firstLetters = (arr) => arr.map(item => item[0]);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named findHappiness that takes in an array of strings and returns an array containing only the strings from the input array that contain ":)".
For example, ['this is great :)', 'wow', 'whyyyyyy :(', ':)))))'] returns ['this is great :)', ':)))))']
------------------------------------------------------------------------------------------------ */
const findHappiness = (arr) => arr.filter(item => item.includes(':)'));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named standardizePhoneNumbers that takes in an array of phone number strings in (XXX) XXX-XXXX format and returns an array with the phone number strings in XXXXXXXXXX format.
For example, (123) 456-7890 returns 1234567890
------------------------------------------------------------------------------------------------ */
const standardizePhoneNumbers = (arr) => arr.map(item => item.replace(/[(]|[)]| |-/g, ''));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named onlyOddChars that takes in a string and returns only the odd-index characters from that string.
For example, 'abcdefg' returns 'bdf'
------------------------------------------------------------------------------------------------ */
const onlyOddChars = (str) => str.split('').filter((char, i) => i % 2 !== 0).join('');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named allHappy that takes in an array of strings and returns a Boolean indicating whether all those strings contain ":)".
------------------------------------------------------------------------------------------------ */
const allHappy = (arr) => {
let allHappy = true;
for(let i = 0; i < arr.length; i++) {
if(!arr[i].includes(':)')){
allHappy = false;
break;
}
}
return allHappy;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named findAnything that takes in an array of strings, along with a target string. Return an array containing only those strings from the original array that contain the target string.
------------------------------------------------------------------------------------------------ */
const findAnything = (arr, target) => arr.filter(item => item.includes(target));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named findEvery that takes in an array of strings, along with a target string. Return a Boolean based on whether or not every string in the array contains the target string.
------------------------------------------------------------------------------------------------ */
const findEvery = (arr, target) => {
let found = true;
for(let i = 0; i < arr.length; i++) {
if(!arr[i].includes(target)){
found = false;
break;
}
}
return found;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
We've been testing a new course enrollment system, and we think we have the bugs worked out, but in the meantime, Brook enrolled himself in a bunch of different classes to test if it was working.
Write a function named unenrollBrook that takes in a two-dimensional array, where each array represents one course's roster and is an array of strings of the names of the people in that course.
Return a two-dimensional array with the same roster, but where anyone whose name includes Brook is removed from every course.
For example, [['Brook Testing', 'Actual Person'], ['Human Person', 'Brook again', 'still Brook']] returns [['Actual Person'], ['Human Person']]
------------------------------------------------------------------------------------------------ */
const unenrollBrook = (arr) => {
let returnArray = [];
arr.map(item => {
let filtered = item.filter(entry => !/Brook/gi.test(entry));
returnArray.push(filtered);
});
return returnArray;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named sortByDay that takes in an array of strings, each of which represents an event's day and time.
Return a two-dimensional array that organizes those strings based on the day on which they occur. For example, all events on Monday are in the first array, all events on Tuesday are in the second array, etc.
If an event takes place on multiple days (i.e. "Dancing on Mondays and Tuesdays"), it should appear in both arrays.
For example, ['Tuesday', 'Monday', 'Wednesday and Thursday', 'Tuesday 2', 'Thursday'] returns
[
['Monday'],
['Tuesday', 'Tuesday 2'],
['Wednesday and Thursday'],
['Wednesday and Thursday', 'Thursday'],
[],
[],
[]
]
------------------------------------------------------------------------------------------------ */
const daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
// TODO: Not working with complex strings
const sortByDay = (arr) => arr.sort((a,b) => daysOfWeek.indexOf(a) - daysOfWeek.indexOf(b));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 11 - Stretch Goal
Write a function named characterByIndex that takes in an array of strings and returns an array containing the first character of the first string, the second character of the second string, etc.
For example, ['abcd', 'efgh', 'ijkl', 'mnop'] returns ['a', 'f', 'k', 'p']
------------------------------------------------------------------------------------------------ */
const characterByIndex = (arr) => arr.map((item, index) => item[index]);
/* ------------------------------------------------------------------------------------------------
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-13.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return an index position of the longest string', () => {
const strArray1 = ['Ginger', 'Goose', 'Tangerine', 'Rosie', 'Mario', 'Malaki']
const strArray2 = [];
const strArray3= ['Ginger']
expect(longestString(strArray1)).toStrictEqual(2);
expect(longestString(strArray2)).toStrictEqual(-1);
expect(longestString(strArray3)).toStrictEqual(0);
});
});
describe('Testing challenge 2', () => {
test('It should return the first letter of each element of the array', () => {
const words = ['apple', 'banana', 'cantaloupe'];
expect(firstLetters(words)).toStrictEqual(['a', 'b', 'c']);
expect(firstLetters(['a', 'b', 'c', 'd'])).toStrictEqual(['a', 'b', 'c', 'd']);
expect(firstLetters([])).toStrictEqual([]);
});
});
describe('Testing challenge 3', () => {
test('It should return only the strings that contain smiley faces', () => {
const words = ['things', 'apple (:)', ':)banana', 'missing that thing', 'cant:)aloupe'];
expect(findHappiness(words)).toStrictEqual(['apple (:)', ':)banana', 'cant:)aloupe']);
expect(findHappiness([])).toStrictEqual([]);
expect(findHappiness(['sadness'])).toStrictEqual([]);
expect(findHappiness([':) yay', ':( no', '', '', '', ''])).toStrictEqual([':) yay']);
});
});
describe('Testing challenge 4', () => {
test('It should return a standardized set of phone numbers', () => {
const nums = ['(123) 456-7890', '(222) 222-2222'];
expect(standardizePhoneNumbers(nums)).toStrictEqual(['1234567890', '2222222222']);
expect(standardizePhoneNumbers([nums[0]])).toStrictEqual(['1234567890']);
});
});
describe('Testing challenge 5', () => {
test('It should only return the odd indexed characters from the string', () => {
expect(onlyOddChars('0123456789')).toStrictEqual('13579');
expect(onlyOddChars('abcd')).toStrictEqual('bd');
expect(onlyOddChars('a')).toStrictEqual('');
expect(onlyOddChars('')).toStrictEqual('');
});
});
describe('Testing challenge 6', () => {
test('It should correctly assess whether all the strings are happy', () => {
const words = ['things', 'apple (:)', ':)banana', 'missing that thing', 'cant:)aloupe'];
expect(allHappy(words)).toStrictEqual(false);
expect(allHappy(['apple (:)', ':)banana', 'cant:)aloupe'])).toStrictEqual(true);
expect(allHappy([])).toStrictEqual(true);
});
});
describe('Testing challenge 7', () => {
test('It should find all the strings that contain a given string', () => {
const words = ['things', 'apple (:)', ':)banana', 'missing that thing', 'cant:)aloupe'];
expect(findAnything(words, ':)')).toStrictEqual(findHappiness(words));
expect(findAnything(words, 'i')).toStrictEqual(['things', 'missing that thing']);
});
});
describe('Testing challenge 8', () => {
test('It should determine whether all the strings contain a given string', () => {
const words = ['things', 'apple pie (:)', ':)banana pie', 'missing that thing', 'cant:)aloupe is tasty'];
expect(findEvery(words, 'a')).toStrictEqual(false);
expect(findEvery(words, '')).toStrictEqual(true);
expect(findEvery(words, 'i')).toStrictEqual(true);
});
});
describe('Testing challenge 9', () => {
test('It should remove Brook from all courses', () => {
const roster = [
['Michelle', 'Allie', 'Brook TESTING'],
['Brook Riggio', 'hey look it\'s Brook', 'Jennifer'],
['Nicholas', 'Sam', 'Scott', 'Vinicio']
];
expect(unenrollBrook(roster)).toStrictEqual([
['Michelle', 'Allie'],
['Jennifer'],
['Nicholas', 'Sam', 'Scott', 'Vinicio']
]);
expect(unenrollBrook([['Brook', 'person'], [], ['person', 'person', 'Brook']])).toStrictEqual([['person'], [], ['person', 'person']]);
expect(unenrollBrook([])).toStrictEqual([]);
});
});
xdescribe('Testing challenge 10', () => {
test('It should sort events by the day on which they happen', () => {
const events = ['Dancing on Mondays and Tuesdays', 'Meet the inventors! Monday, August 7', 'in the club on a Tuesday', 'Thursday Night Code', 'Saturday Night Fever'];
const sortedEvents = sortByDay(events);
expect(sortedEvents[0]).toEqual(expect.arrayContaining(['Dancing on Mondays and Tuesdays', 'Meet the inventors! Monday, August 7']));
expect(sortedEvents[1]).toEqual(expect.arrayContaining(['Dancing on Mondays and Tuesdays', 'in the club on a Tuesday']));
expect(sortedEvents[2]).toStrictEqual([]);
expect(sortedEvents[3]).toStrictEqual(['Thursday Night Code']);
expect(sortedEvents[4]).toStrictEqual([]);
expect(sortedEvents[5]).toStrictEqual(['Saturday Night Fever']);
expect(sortedEvents[6]).toStrictEqual([]);
const events2 = ['Tuesday', 'Monday', 'Wednesday and Thursday', 'Tuesday 2', 'Thursday'];
const sortedEvents2 = sortByDay(events2);
expect(sortedEvents2[0]).toStrictEqual(['Monday']);
expect(sortedEvents2[1]).toEqual(expect.arrayContaining(['Tuesday', 'Tuesday 2']));
expect(sortedEvents2[2]).toStrictEqual(['Wednesday and Thursday']);
expect(sortedEvents2[3]).toEqual(expect.arrayContaining(['Wednesday and Thursday', 'Thursday']));
expect(sortedEvents2[4]).toStrictEqual([]);
expect(sortedEvents2[5]).toStrictEqual([]);
expect(sortedEvents2[6]).toStrictEqual([]);
});
});
describe('Testing challenge 11', () => {
test('It should return the ith character of the ith string', () => {
const words = ['apple', 'banana', 'cantaloupe'];
expect(characterByIndex(words)).toStrictEqual(['a', 'a', 'n']);
expect(characterByIndex(['abc', 'def', 'ghi'])).toStrictEqual(['a', 'e', 'i']);
expect(characterByIndex(['wow', 'wow', 'wow'])).toStrictEqual(['w', 'o', 'w']);
});
});