-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-14.test.js
311 lines (259 loc) · 10.5 KB
/
challenges-14.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named screenForNames that takes in an array of strings and uses Regex to create a new array of only those strings that match the following rules:
* the name must begin with Mr., Mrs., Ms., Dr. followed by a space
* the name must contain only letter characters (white spaces are ok)
------------------------------------------------------------------------------------------------ */
const screenForNames = (arr) => arr.filter(item => /^(Mr|Mrs|Ms|Dr)[.]\s\w/g.test(item));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named toTitleCase that takes in an array of strings and returns an array of strings with the first character in upper case and the rest as is.
For example, ['apple', 'banana', 'MacGyver'] returns ['Apple', 'Banana', 'MacGyver'].
------------------------------------------------------------------------------------------------ */
const toTitleCase = (arr) => arr.map(item => item.slice(0, 1).toUpperCase() + item.slice(1, item.length));
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named biggerThanLuke that, given the Star Wars data, below, returns the names of the characters whose mass is greater than Luke's.
The names should be combined into a single string with each character name separated by a dash.
For example, "Lando Calrisian - Boba Fett - Princess Amidala".
------------------------------------------------------------------------------------------------ */
let starWarsData = [{
name: 'Luke Skywalker',
height: '172',
mass: '77',
hair_color: 'blond',
skin_color: 'fair',
eye_color: 'blue',
birth_year: '19BBY',
gender: 'male',
},
{
name: 'C-3PO',
height: '167',
mass: '75',
hair_color: 'n/a',
skin_color: 'gold',
eye_color: 'yellow',
birth_year: '112BBY',
gender: 'n/a'
},
{
name: 'R2-D2',
height: '96',
mass: '32',
hair_color: 'n/a',
skin_color: 'white, blue',
eye_color: 'red',
birth_year: '33BBY',
gender: 'n/a'
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
hair_color: 'none',
skin_color: 'white',
eye_color: 'yellow',
birth_year: '41.9BBY',
gender: 'male'
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
hair_color: 'brown',
skin_color: 'light',
eye_color: 'brown',
birth_year: '19BBY',
gender: 'female'
},
{
name: 'Pex Kylar',
height: '180',
mass: '190',
hair_color: 'orange',
skin_color: 'brown',
eye_color: 'none',
birth_year: '27BBY',
gender: 'n/a'
}];
let biggerThanLuke = (arr) => {
let luke = arr.filter(item => item.name === 'Luke Skywalker');
let charsBiggerThanLuke = arr.reduce((a,b) => +b.mass > +luke[0].mass ? a.concat(b.name) : a, []).join(' - ');
return charsBiggerThanLuke;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
Write a function named sortBy that takes in an array of objects, each of which has a particular property, and sorts those objects by that property, 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}
];
This data could be sorted by name or price.
------------------------------------------------------------------------------------------------ */
const sortBy = (property, arr) => property === 'name' ? arr.sort((a,b) => a[property].localeCompare(b[property])) : arr.sort((a,b) => a[property] > b[property] ? 1 : -1);
// // I'd argue this is actually better code
// const sortBy = (property, arr) => {
// let result;
// if(property === 'name') {
// result = arr.sort((a,b) => a.name.localeCompare(b.name));
// }
// else if(property === 'price') {
// result = arr.sort((a,b) => (a[property] > b[property]) ? 1 : -1);
// } else {
// console.warn('Please Enter a Valid Property to Sort By');
// }
// console.log(`Sorted By: ${property}`, result);
// return result;
// };
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function that determines if a given URL is secure, beginning with https://
Guard against malformed URLs, such as: https:missing-slashes.bad
For example:
http://www.insecure.com returns false because the URL is not secure
https://secure.com returns true because the URL is secure
https:/missingslash.org returns false because the URL is malformed
------------------------------------------------------------------------------------------------ */
const isSecure = (url) => /^https:\/\//g.test(url);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named detectTicTacToeWin that accepts a two-dimensional array of strings. Each string is guaranteed to be either "X", "O" or an empty string. Your function should check to see if any row, column, or either diagonal direction has three matching "X" or "O" symbols (non-empty strings), three-in-a-line.
This function should return either true or false to indicate if someone won the game.
Instead of trying to write crazy for loops to automate checking the rows, columns and diagonals consider writing one helper function that accepts three coordinate pairs and checks the values of the array at those locations. For instance helpCheck(row1, col1, row2, col2, row3, col3).
Your function does not need to work for boards of any size other than 3x3.
Here is a sample board:
[
['X', '', 'O'],
['X', 'O', ''],
['X', 'X', 'X'],
];
------------------------------------------------------------------------------------------------ */
const detectTicTacToeWin = (board) => {
// Use a magic square: https://mathworld.wolfram.com/MagicSquare.html
let magicSquare = [
[8, 1, 6],
[3, 5, 7],
[4, 9, 2]
];
// Game Status variable
let gameOver = false;
// Iterate through each row
for(let i=0; i < board.length; i++) {
let totals = {
'horizontalX': 0,
'horizontalO': 0,
'verticalX': 0,
'verticalO': 0,
'diagonalX': 0,
'diagonalO': 0,
'antiDiagonalX': 0,
'antiDiagonalO': 0
};
// Iterate through each square in the row
for(let j=0; j < board[i].length; j++) {
// Vertical Check
if(board[i][j] === 'X'){
totals.horizontalX += magicSquare[i][j];
} else if(board[i][j] === 'O') {
totals.horizontalO += magicSquare[i][j];
}
// Horizontal Check
if(board[j][i] === 'X') {
totals.verticalX += magicSquare[j][i];
} else if (board[j][i] === 'O') {
totals.verticalO += magicSquare[j][i];
}
// Check Diagonal
if(board[j][j] === 'X') {
totals.diagonalX += magicSquare[j][j];
} else if (board[j][j] === 'O') {
totals.diagonalO += magicSquare[j][j];
}
// Check Anti-Diagonal
if(board[j][board[i].length - j -1] === 'X') {
totals.antiDiagonalX += magicSquare[j][board[i].length - j -1];
} else if (board[j][board[i].length - j -1] === 'O') {
totals.antiDiagonalO += magicSquare[j][board[i].length - j -1];
}
}
if(Object.values(totals).includes(15)) {
gameOver = true;
break;
}
}
return gameOver;
};
/* ------------------------------------------------------------------------------------------------
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 challenge-14.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('It should return a list of names', () => {
const names = ['Mr. Brown', ' Ms. Red', 'Dr. Blue', 'Mrs.', '', 'Ms. Black', 'dr. Green', 'Mrs. Orange', 'Purple', 'Mr. Pink'];
expect(screenForNames(names)).toStrictEqual(['Mr. Brown', 'Dr. Blue', 'Ms. Black', 'Mrs. Orange']);
});
});
describe('Testing challenge 2', () => {
test('It should convert each word to title case', () => {
const words = ['apple', 'banana', 'MacGyver'];
expect(toTitleCase(words)).toStrictEqual(['Apple', 'Banana', 'MacGyver']);
expect(toTitleCase([])).toStrictEqual([]);
});
});
describe('Testing challenge 3', () => {
test('It should return only characters that are bigger than Luke', () => {
expect(biggerThanLuke(starWarsData)).toStrictEqual('Darth Vader - Pex Kylar');
expect(biggerThanLuke([])).toStrictEqual('');
});
});
describe('Testing challenge 4', () => {
test('It should sort items by a price', () => {
expect(sortBy('price', [
{ 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 },
]);
});
test('It should sort items by name', () => {
expect(sortBy('name', [
{ name: 'Sweatshirt', price: 45 },
{ name: 'Bookmark', price: 2.50 },
{ name: 'Tote bag', price: 15 }
])).toStrictEqual([
{ name: 'Bookmark', price: 2.50 },
{ name: 'Sweatshirt', price: 45 },
{ name: 'Tote bag', price: 15 },
]);
});
});
describe('Testing challenge 5', () => {
test('It should check if url is https', () => {
expect(isSecure('http://www.insecure.com')).toBe(false);
expect(isSecure('https://secure.com')).toBe(true);
expect(isSecure('https:/missingslash.org')).toBe(false);
});
});
describe('Testing challenge 6', () => {
test('It should return true if there are three in a row', () => {
expect(detectTicTacToeWin([['X', '', 'O'], ['X', 'O', ''], ['X', 'O', 'X']])).toStrictEqual(true);
expect(detectTicTacToeWin([['O', '', 'X'], ['X', 'O', 'X'], ['X', '', 'O']])).toStrictEqual(true);
expect(detectTicTacToeWin([['O', 'O', 'O'], ['X', '', 'X'], ['X', 'X', 'O']])).toStrictEqual(true);
expect(detectTicTacToeWin([['O', '', 'X'], ['O', 'X', ''], ['X', 'O', '']])).toStrictEqual(true);
});
test('It should return false if there are not three in a row', () => {
expect(detectTicTacToeWin([['X', '', 'O'], ['O', 'O', ''], ['X', 'O', 'X']])).toStrictEqual(false);
});
test('It should not treat empty 3 in row as winner', () => {
expect(detectTicTacToeWin([['', '', ''], ['O', 'O', ''], ['X', 'O', 'X']])).toEqual(false);
});
});