-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-14.test.js
244 lines (201 loc) · 7.56 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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1
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) => {
// Solution code here...
const upper = arr.map(value => {
const upperCase = value[0].toUpperCase() + value.slice(1);
return upperCase;
});
return upper;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
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) => {
// Solution code here...
const namesArray = [];
arr.map(person => {
console.log(person.mass);
if (parseInt(person.mass) > 77)
namesArray.push(person.name);
});
return namesArray.join(' - ');
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
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) => {
// Solution code here...
let answer = arr.sort(function(a, b){
if(a[property] < b[property]) return -1;
if(a[property] > b[property]) return 1;
});
return answer;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
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) => {
// Solution code here...
let regex = /https:\/\//gm;
return regex.test(url);
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5 - Stretch Goal
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', 'O', 'X'],
];
------------------------------------------------------------------------------------------------ */
const detectTicTacToeWin = (board) => {
// Solution code here...
};
/* ------------------------------------------------------------------------------------------------
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 convert each word to title case', () => {
const words = ['apple', 'banana', 'MacGyver'];
expect(toTitleCase(words)).toStrictEqual(['Apple','Banana','MacGyver']);
expect(toTitleCase([])).toStrictEqual([]);
});
});
describe('Testing challenge 2', () => {
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 3', () => {
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 4', () => {
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);
});
});
xdescribe('Testing challenge 5', () => {
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);
});
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);
});
});