-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges-09.test.js
284 lines (217 loc) · 10 KB
/
challenges-09.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
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function that finds the maximum value in an array
using the 'reduce' method.
E.g. [4,2,7,5,9,2] -> 9
------------------------------------------------------------------------------------------------ */
const maxInArray = (arr) => arr.reduce((a, b) => a < b ? b : a , 0);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named getCourseKeys that takes in the courseInfo object and returns an array containing the keys for the courseInfo object.
For example: (['name', 'duration', 'topics', 'finalExam']).
------------------------------------------------------------------------------------------------ */
const courseInfo = { name: 'Code 301', duration: { dayTrack: '4 weeks', eveningTrack: '8 weeks'},
topics: ['SMACSS', 'APIs', 'NodeJS', 'SQL', 'jQuery', 'functional programming'],
finalExam: true
};
const getCourseKeys = (obj) => Object.keys(obj);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named checkValues that takes in an object and a value and returns true if the value is in the object.
------------------------------------------------------------------------------------------------ */
const checkValues = (obj, value) => Object.values(obj).includes(value);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
You are given an object with names and their coresponding phone numbers that looks like this:
{
'Grace Hopper': '222-303-5938',
'Ada Lovelace': '222-349-9842',
'Alan Turing': '222-853-5933'
}
HR has asked you to change the data to make it easier to print so that it looks like this:
{
'Grace Hopper: 222-303-5938',
'Ada Lovelace: 222-349-9842',
'Alan Turing: 222-853-5933'
}
------------------------------------------------------------------------------------------------ */
const updateNumbers = (obj) => Object.entries(obj).map(item => `${item[0]}: ${item[1]}`);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Write a function named getHouses that returns a new array containing the names of all of the houses in the data set.
------------------------------------------------------------------------------------------------ */
const characters = [
{
name: 'Eddard',
spouse: 'Catelyn',
children: ['Robb', 'Sansa', 'Arya', 'Bran', 'Rickon'],
house: 'Stark',
},
{
name: 'Jon',
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: 'Sansa',
spouse: 'Tyrion',
house: 'Stark',
},
{
name: 'Jon',
spouse: null,
house: 'Snow',
},
];
const getHouses = (arr) => arr.map(entry => entry.house);
/*------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named hasChildrenValues that uses Object.values to determine if any given character in the data set has children.
This function should take in an array of data and a character name and return a Boolean.
For example:
hasChildrenValues(characters, 'Cersei') will return true
hasChildrenValues(characters, 'Sansa') will return false
------------------------------------------------------------------------------------------------ */
const hasChildrenValues = (arr, character) => {
let results = arr.filter(item => item.name === character);
return Boolean(Object.values(results)[0].children);
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named hasChildrenEntries that is similar to your hasChildrenValues function from challenge 4, but uses the data's entries instead of its values.
The input and output of this function are the same as the input and output from challenge 3.
------------------------------------------------------------------------------------------------ */
const hasChildrenEntries = (arr, character) => {
let results = arr.filter(item => item.name === character);
if(results.length !== 0) {
return 'children' in results[0];
} else {
return false;
}
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named totalCharacters that takes in an array and returns the number of characters in the array.
------------------------------------------------------------------------------------------------ */
const totalCharacters = (arr) => arr.filter(item => !item.house).length;
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Write a function named houseSize that takes in the array of characters and creates an object for each house containing the name of the house and the number of members.
All of these objects should be added to an array named "sizes". Return the "sizes" array from the function.
For example: [{ house: 'Stark', members: 7 }, { house: 'Arryn', members: 3 }, ... ].
------------------------------------------------------------------------------------------------ */
const houseSize = (arr) => {
const sizes = [];
// Solution code here...
return sizes;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
As fans are well aware, "When you play the game of thrones, you win or you die. There is no middle ground."
We will assume that Alerie Tyrell is deceased. She missed her daughter's wedding. Twice.
Write a function named houseSurvivors. You may modify your houseSize function from challenge 6 to use as the basis of this function.
This function should create an object for each house containing the name of the house and the number of members. If the spouse is deceased, do not include him/her in the total number of family members.
All of these objects should be added to an array named "survivors". Return the "survivors" array from the function.
For example: [ { house: 'Stark', members: 6 }, { house: 'Arryn', members: 2 }, ... ].
------------------------------------------------------------------------------------------------ */
const deceasedSpouses = ['Catelyn', 'Lysa', 'Robert', 'Khal Drogo', 'Alerie'];
const houseSurvivors = (arr) => {
const survivors = [];
// Solution code here...
return survivors;
};
/* ------------------------------------------------------------------------------------------------
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-06.test.js
------------------------------------------------------------------------------------------------ */
describe.only('Testing challenge 1', () => {
test('It should return the maximum number found', () => {
expect(maxInArray([4, 2, 7, 5, 9, 2])).toStrictEqual(9);
});
test('It should handle negatives and return the maximum number found', () => {
expect(maxInArray([4, -2, -7, 5, -9, 2])).toStrictEqual(5);
});
});
describe.only('Testing challenge 2', () => {
test('It should return the keys from an object', () => {
expect(getCourseKeys(courseInfo)).toStrictEqual(['name', 'duration', 'topics', 'finalExam']);
});
});
describe.only('Testing challenge 3', () => {
test('It should return true if the value is in the object', () => {
expect(checkValues({ class: '301' }, '301')).toBe(true);
});
test('It should return false if the value is not in the object', () => {
expect(checkValues({ class: '301' }, '401')).toBe(false);
});
});
describe.only('Testing challenge 4', () => {
test('It should return an an array of names and numbers', () => {
const startingObj = {
'Grace Hopper': '222-303-5938',
'Ada Lovelace': '222-349-9842',
'Alan Turing': '222-853-5933'
};
expect(updateNumbers(startingObj).includes('Grace Hopper: 222-303-5938')).toBe(true);
});
});
describe.only('Testing challenge 5', () => {
test('It should return an array of the names of the houses', () => {
expect(getHouses(characters)[0]).toStrictEqual('Stark');
expect(getHouses(characters).length).toStrictEqual(7);
});
});
describe.only('Testing challenge 6', () => {
test('It should return true for characters that have children', () => {
expect(hasChildrenValues(characters, 'Daenarys')).toBeTruthy();
});
test('It should return false to characters who do not have children', () => {
expect(hasChildrenValues(characters, 'Sansa')).toBeFalsy();
});
});
describe.only('Testing challenge 7', () => {
test('It should return true for characters that have children', () => {
expect(hasChildrenEntries(characters, 'Eddard')).toBeTruthy();
});
test('It should return false to characters who do not have children', () => {
expect(hasChildrenEntries(characters, 'Jon S.')).toBeFalsy();
});
});
xdescribe('Testing challenge 8', () => {
test('It should return the number of characters in the array', () => {
expect(totalCharacters(characters)).toStrictEqual(27);
});
});
xdescribe('Testing challenge 9', () => {
test('It should return an object for each house containing the name and size', () => {
expect(houseSize(characters)[1]).toStrictEqual({ house: 'Arryn', members: 3 });
expect(houseSize(characters).length).toStrictEqual(7);
});
});
xdescribe('Testing challenge 10', () => {
test('It should not include any deceased spouses', () => {
expect(houseSurvivors(characters)[2]).toStrictEqual({ house: 'Lannister', members: 4 });
});
});