-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-08.test.js
304 lines (229 loc) · 14.4 KB
/
challenges-08.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Use the characters data below for all of the challenges except challenge 2 and 3.
Write a function named sortByChildren that sorts the characters below by the number of children in each house (fewest to most). If a house has the same number of children, sort alphabetically by house name.
------------------------------------------------------------------------------------------------ */
let characters = [
{
name: 'Eddard',
spouse: 'Catelyn',
children: ['Robb', 'Sansa', 'Arya', 'Bran', 'Rickon'],
house: 'Stark'
},
{
name: 'Jon A.',
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: 'Euron',
spouse: null,
children: [],
house: 'Greyjoy'
},
{
name: 'Jon S.',
spouse: null,
children: [],
house: 'Snow'
}
];
const sortByChildren = (charArray) => charArray.sort((a,b) => a.children.length === b.children.length ? a.house.localeCompare(b.house) : a.children.length - b.children.length);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named containsW that takes in a string. This function should use a regular expression pattern to return true if the string contains the letter 'w' in lower case or false if it does not.
------------------------------------------------------------------------------------------------ */
const containsW = (str) => /w/.test(str);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named isNum that takes in a string or number of any length. This function should use a regular expression pattern to return true if the input contains a number, and false if the input does not contain a number.
For example:
12345 returns true
'12345' returns true
'h3llo world' returns true
'hello world' returns false
------------------------------------------------------------------------------------------------ */
const isNum = (input) => /\d/.test(input);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named containsWorld that takes in a string or number of any length. This function should use a regular expression pattern to return true if the input contains the word 'world' all in lower-case letters, and false if the input does not.
------------------------------------------------------------------------------------------------ */
const containsWorld = (input) => /world/g.test(input);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named isCapitalized that takes in a string. This function should use a regular expression pattern to match all words that begin with a capital letter. It should only match words, not punctuation.
Return an array containing all the matches.
------------------------------------------------------------------------------------------------ */
// const isCapitalized = (str) => [...str.matchAll(/\b[A-Z]\w*\b/g)].map(items => items[0]);
const isCapitalized = str => str.match(/\b[A-Z]\w*\b/g) || [];
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named citiesAtoJ that takes in an array of city names and uses a regular expression pattern to return a new array containing any cities that begin with the letters A through J, inclusive.
------------------------------------------------------------------------------------------------ */
const citiesAtoJ = (arr) => arr.filter(item => /^[A-J]/g.test(item));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
You have created a game application and begin by asking users an easy question: In which month is Halloween?
Write a function named matchMonth which uses a regular expression pattern to match any of these inputs: October, Oct, october, oct
If the user enters any of these four inputs, return true. For any other input, return false.
Do not use the vertical bar (pipe) in your pattern.
------------------------------------------------------------------------------------------------ */
// With Pipes :/
// const matchMonth = (input) => /^October$|^Oct$|^october$|^oct$/g.test(input);
const matchMonth = input => /^[Oo]ct(ober)?$/g.test(input);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named noPunctuation that contains a regular expression pattern to find all of the words that contain a space immediately at the end of the word. Return an array of all such words, still containing the space at the end.
For example, if given the string "Hello, and have a wonderful day!", the word "Hello, " would not be returned because it is immediately followed by a comma. The word "day!" would not be returned because it is immediately followed by an exclamation point.
The expected output of "Hello, and have a wonderful day!" is ["and ", "have ", "a ", "wonderful "].
------------------------------------------------------------------------------------------------ */
// const noPunctuation = str => [...str.matchAll(/\b\w*\s/g)].map(items => items[0]);
const noPunctuation = str => str.match(/\b\w*\s/g) || [];
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
You want to teach a friend how to play hangman and want to show them using a partially complete puzzle.
Write a function named hangman which uses the replace method to remove all of the vowels (a, e, i, o, u) from the hangman string, regardless of capitalization, and replace them with an underscore.
The function should return a string containing the consonants in their original positions and underscores where the vowels were previously located.
For example, 'Welcome to Code 301!' will return 'W_lc_m_ t_ C_d_ 301!'.
------------------------------------------------------------------------------------------------ */
let hangman = (str) => str.replace(/[aeiou]/ig, '_');
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named findShells that takes in the string below and uses a regular expression pattern to find all instances of the following words: "sells", "shells", "seashells".
Do not use the vertical bar (pipe) character.
Hint: All of these words end with the letters "ells".
------------------------------------------------------------------------------------------------ */
const seashells = 'She sells seashells by the seashore. The shells she sells are surely seashells. So if she sells shells on the seashore, I\'m sure she sells seashore shells.';
// const findShells = (str) => [...str.matchAll(/\b\w*ells\b/g)].map(items => items[0]);
const findShells = str => str.match(/\b\w*ells\b/g) || [];
/* ------------------------------------------------------------------------------------------------
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-04.solution.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should sort the characters by number of children', () => {
expect(sortByChildren(characters)[0].name).toStrictEqual('Euron');
expect(sortByChildren(characters)[0].children.length).toStrictEqual(0);
});
});
describe('Testing challenge 2', () => {
test('It should return true if the input contains a lower case w', () => {
expect(containsW('hello world')).toBe(true);
});
test('It should return false if the input contains an upper case W', () => {
expect(containsW('Hello World')).toBe(false);
});
test('It should return false if the input does not contain a w', () => {
expect(containsW('hello everyone')).toBe(false);
});
});
describe('Testing challenge 3', () => {
test('It should return true if the input is a number', () => {
expect(isNum(1234567890)).toBeTruthy();
expect(isNum('12345')).toBeTruthy();
});
test('It should return true if the input contains a number', () => {
expect(isNum('h3llo w0rld')).toBeTruthy();
});
test('It should return false if the input does not contain a number', () => {
expect(isNum('hello world')).toBeFalsy();
expect(isNum('')).toBeFalsy();
});
});
describe('Testing challenge 4', () => {
test('It should return true if the input contains the word school in lower case', () => {
expect(containsWorld('hello world')).toBe(true);
});
test('It should return false if the input contains the word school with any upper case letters', () => {
expect(containsWorld('Hello World')).toBe(false);
});
test('It should return false if the input does not contain the word school', () => {
expect(containsWorld('hello everyone')).toBe(false);
});
});
describe('Testing challenge 5', () => {
test('It should only return words that begin with a capital letter', () => {
const capitalResult = isCapitalized('We only want to Return the Words that begin With a capital Letter');
expect(capitalResult).toStrictEqual([ 'We', 'Return', 'Words', 'With', 'Letter' ]);
expect(capitalResult.length).toStrictEqual(5);
expect(isCapitalized('Given by our hand in the meadow that is called Runnymede, between Windsor and Staines, on the fifteenth day of June in the seventeenth year of our reign (i.e. 1215: the new regnal year began on 28 May).')).toStrictEqual(['Given', 'Runnymede', 'Windsor', 'Staines', 'June', 'May']);
expect(isCapitalized('these words are all failures')).toStrictEqual([]);
});
});
describe('Testing challenge 6', () => {
let cities = ['Cleveland', 'San Diego', 'Birmingham', 'Seattle', 'Miami', 'New York City', 'Omaha', 'Portland', 'Austin', 'Boston', 'Newport Beach', 'Hoboken'];
test('It should return the cities whose names begin with the letters A through J', () => {
expect(citiesAtoJ(cities)).toContain('Cleveland', 'Birmingham', 'Austin', 'Boston', 'Hoboken');
expect(citiesAtoJ(cities).length).toStrictEqual(5);
expect(citiesAtoJ([])).toStrictEqual([]);
expect(citiesAtoJ(['Albuquerque', 'Chicago', 'Philadelphia', 'Newark', 'Sacramento', 'Eugene'])).toEqual(expect.arrayContaining(['Albuquerque', 'Chicago', 'Eugene']));
});
test('It should not return the cities whose names begin with the letters K through Z', () => {
expect(citiesAtoJ(cities)).not.toContain('San Diego', 'Seattle', 'Miami', 'New York City', 'Omaha', 'Portland', 'Newport Beach');
});
});
describe('Testing challenge 7', () => {
test('It should match any of the acceptable inputs', () => {
expect(matchMonth('Oct')).toBeTruthy();
expect(matchMonth('oct')).toBeTruthy();
expect(matchMonth('October')).toBeTruthy();
expect(matchMonth('october')).toBeTruthy();
});
test('It should not match anything other than the acceptable inputs', () => {
expect(matchMonth('November')).toBeFalsy();
expect(matchMonth('nov')).toBeFalsy();
expect(matchMonth(123)).toBeFalsy();
expect(matchMonth('octob')).toBeFalsy();
expect(matchMonth('OCTOBER')).toBeFalsy();
expect(matchMonth('notOctober')).toBeFalsy();
});
});
describe('Testing challenge 8', () => {
const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lacinia vel massa sed egestas. Nunc faucibus iaculis elit, a scelerisque enim condimentum sed. Aenean ac scelerisque sem, et pharetra diam.';
test('It should only return words that are immediately followed by a space', () => {
expect(noPunctuation(lorem)).toStrictEqual([ 'Lorem ', 'ipsum ', 'dolor ', 'sit ', 'consectetur ', 'adipiscing ', 'Cras ', 'lacinia ', 'vel ', 'massa ', 'sed ', 'Nunc ', 'faucibus ', 'iaculis ', 'a ', 'scelerisque ', 'enim ', 'condimentum ', 'Aenean ', 'ac ', 'scelerisque ', 'et ', 'pharetra ' ]);
expect(noPunctuation(lorem).length).toStrictEqual(23);
expect(noPunctuation('Given by our hand in the meadow that is called Runnymede, between Windsor and Staines, on the fifteenth day of June in the seventeenth year of our reign (i.e. 1215: the new regnal year began on 28 May).')).toEqual(expect.arrayContaining(['Given ', 'by ', 'our ', 'hand ', 'in ', 'the ', 'meadow ', 'that ', 'is ', 'called ', 'between ', 'Windsor ', 'and ', 'on ', 'the ', 'fifteenth ', 'day ', 'of ', 'June ', 'in ', 'the ', 'seventeenth ', 'year ', 'of ', 'our ', 'reign ', 'the ', 'new ', 'regnal ', 'year ', 'began ', 'on ', '28 ']));
});
test('It should not contain words that are followed by any non-space character', () => {
expect(noPunctuation(lorem)).not.toContain(['amet,', 'elit.', 'egestas.', 'elit,', 'sed.', 'sem,', 'diam.', 'nibh.', 'porttitor.', 'euismod,', 'ultrices.', 'massa,', 'vel,', 'purus.', 'purus,', 'odio.', 'aliquet,', 'non,', 'sem.']);
});
});
describe('Testing challenge 9', () => {
let startString = 'This is a regex challenge. We are trying to create a hangman phrase where all of the vowels are missing!';
test('It should remove the vowels from the hangman string and replace them with underscores', () => {
expect(hangman(startString)).toStrictEqual('Th_s _s _ r_g_x ch_ll_ng_. W_ _r_ try_ng t_ cr__t_ _ h_ngm_n phr_s_ wh_r_ _ll _f th_ v_w_ls _r_ m_ss_ng!');
expect(hangman('I wAnt them all tO bE removed and replaced with Underscores.')).toStrictEqual('_ w_nt th_m _ll t_ b_ r_m_v_d _nd r_pl_c_d w_th _nd_rsc_r_s.');
});
test('It should not contain the letters "a", "e", "i", "o", or "u"', () => {
expect(hangman(startString)).not.toContain('a', 'e', 'i', 'o', 'u');
});
});
describe('Testing challenge 10', () => {
test('It should return an array of instances of "sells", shells", and "seashells"', () => {
expect(findShells(seashells)).toStrictEqual(['sells', 'seashells', 'shells', 'sells', 'seashells', 'sells', 'shells', 'sells', 'shells']);
expect(findShells(seashells).length).toStrictEqual(9);
});
});