-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-10.test.js
360 lines (279 loc) · 13.8 KB
/
challenges-10.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use strict';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 1 - Review
Write a function named returnTen, takes in a string and uses split and splice to return the last 10 characters from that string as elements of an array.
------------------------------------------------------------------------------------------------ */
function returnTen(str){
return str.split('').slice(-10);
}
/* ------------------------------------------------------------------------------------------------
CHALLENGE 2
Write a function named findMax that takes in a matrix of positive numbers and returns the number with the highest value.
For example:
[
[1, 3, 4, 5],
[4, 5, 6],
[23, 5, 5]
]
return: 23
------------------------------------------------------------------------------------------------ */
const findMax = (matrix) => {
let currentMax = matrix[0][0];
matrix.forEach(item => {
let arrayMax = item.reduce((a, b) => Math.max(a, b));
if(arrayMax > currentMax) {
currentMax = arrayMax;
}
});
return currentMax;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 3
Write a function named totalSum that takes in a matrix of numbers and returns the totalSum of all the numbers.
For example:
[
[1, 3, 4, 5],
[4, 5, 1],
[2, 5, 5]
]
return: 35
------------------------------------------------------------------------------------------------ */
const totalSum = (matrix) => {
let sum = 0;
matrix.forEach(item => {
let arraySum = item.reduce((a, b) => a + b, 0);
sum += arraySum;
});
return sum;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 4
You friend Pat has a chain of stores around the greater Seattle area. He specializes in selling salmon cookies. Pat has data for the hourly sales of cookies per hour for each store. He wants to create an array of the total number of cookies sold per hour for all of his stores combined.
Write a function named grandTotal that adds up the cookies sales for each hour of operation for all of the stores combined. For example, the first element in the hourlySales array should be the sum of the cookies sold in the 9:00 a.m. hour at all five stores combined.
For this example, the total at 9:00 a.m. is 17 + 26 + 7 + 5 + 33, or 88 total cookies.
Return the array of the total number of cookies sold per hour for all of the stores combined.
------------------------------------------------------------------------------------------------ */
const hoursOpen = ['9 a.m.', '10 a.m.', '11 a.m.', '12 p.m.', '1 p.m.', '2 p.m.', '3 p.m.', '4 p.m.', '5 p.m.', '6 p.m.', '7 p.m.', '8 p.m.'];
const firstPike = [17, 18, 23, 24, 24, 12, 13, 27, 30, 20, 24, 18];
const seaTac = [26, 5, 5, 59, 23, 39, 38, 20, 30, 7, 59, 43];
const seattleCenter = [7, 14, 19, 22, 15, 4, 23, 27, 28, 23, 1, 29];
const capHill = [5, 85, 58, 51, 50, 13, 33, 32, 47, 94, 31, 62];
const alkiBeach = [33, 31, 147, 130, 27, 93, 38, 126, 141, 63, 46, 17];
const cookieStores = [firstPike, seaTac, seattleCenter, capHill, alkiBeach];
const grandTotal = (stores) => {
let hourlyTotals = [];
hoursOpen.forEach((hour, index) => {
let total = 0;
stores.forEach(store => {
total += store[index];
});
hourlyTotals.push(total);
});
return hourlyTotals;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 5
Pat has decided that he would also like to organize his data as objects containing the number of cookies sold per hour and the time.
Here is sample data for the 9:00 sales: { sales: '88 cookies', time: '9 a.m.' }.
Write a function named salesData that uses forEach to iterate over the hourlySales array and create an object for each hour. Return an array of the formatted data.
------------------------------------------------------------------------------------------------ */
const salesData = (hours, data) => {
let totals = [];
hours.forEach((item, index) => totals.push({sales: `${data[index]} cookies`, time: item}));
return totals;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 6
Write a function named howManyTreats that will return the quantity of treats you need to pick up from the pet store today from this array.
------------------------------------------------------------------------------------------------ */
const errands = [
{
store: 'Grocery store',
items: [{ name: 'Eggs', quantity: 12 }, { name: 'Milk', quantity: 1 }, { name: 'Apples', quantity: 3 }]
},
{
store: 'Drug store',
items: [{ name: 'Toothpaste', quantity: 1 }, { name: 'Toothbrush', quantity: 3 }, { name: 'Mouthwash', quantity: 1 }]
},
{
store: 'Pet store',
items: [{ name: 'Cans of food', quantity: 8 }, { name: 'Treats', quantity: 24 }, { name: 'Leash', quantity: 1 }]
}
];
// const howManyTreats = (arr) => arr.filter(store => store.name === 'Pet store ' && store.items.filter(items => Object.values(items).includes('Treats')));
const howManyTreats = (arr) => {
let storeResults = arr.filter(item => item.store === 'Pet store');
let treats = storeResults[0].items.filter(entry => entry.name === 'Treats');
return treats[0].quantity;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 7 - Stretch Goal
Write a function named battleship that accepts a 2D array and two numbers: a row coordinate and a column coordinate.
Return "hit" or "miss" depending on if there's part of a boat at that position in the array. Assume the array has only one of two values at each index. '#' for part of a boat, or ' ' for open water.
Here is a sample board:
[
['#', ' ', '#', ' '],
['#', ' ', '#', ' '],
['#', ' ', ' ', ' '],
[' ', ' ', '#', '#'],
]
The top row of the board is considered row zero and row numbers increase as they go down.
------------------------------------------------------------------------------------------------ */
const battleship = (board, row, col) => board[row][col] === '#' ? 'hit' : 'miss';
/* ------------------------------------------------------------------------------------------------
CHALLENGE 8 - Stretch Goal
Write a function named calculateProduct that takes in a two-dimensional array of numbers, multiplies all of the numbers in each array, and returns the final product. This function should work for any number of inner arrays.
For example, the following input returns a product of 720: [[1,2], [3,4], [5,6]]
------------------------------------------------------------------------------------------------ */
const calculateProduct = (numbers) => numbers.flat().reduce((a,b) => a * b);
/* ------------------------------------------------------------------------------------------------
CHALLENGE 9 - Stretch Goal
Write a function named averageDailyTemperature that accepts a two-dimensional array representing average daily temperatures grouped week-by-week.
Calculate the average daily temperature during that entire period. Your output should be a single number. Write your function so it could accept an array with any number of weeks given to it.
------------------------------------------------------------------------------------------------ */
// Real daily average temperatures for Seattle, October 1-28 2017
const weeklyTemperatures = [
[66, 64, 58, 65, 71, 57, 60],
[57, 65, 65, 70, 72, 65, 51],
[55, 54, 60, 53, 59, 57, 61],
[65, 56, 55, 52, 55, 62, 57],
];
const averageDailyTemperature = (weather) => {
let flatWeather = weather.flat();
return flatWeather.reduce((a,b) => a + b) / flatWeather.length;
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 10 - Stretch Goal
Write a function named lowestWeeklyAverage that accepts a two-dimensional array of daily temperatures grouped week-by-week.
Calculate the average temperature for each week and return the value of the lowest weekly average temperature.
For example, in the data set below, the lowest weekly average is 46, which is the average of the temperatures in week 2. All other weeks have average temperatures that are greater than 46.
------------------------------------------------------------------------------------------------ */
let lowestWeeklyTemperatureData = [
[33, 64, 58, 65, 71, 57, 60],
[40, 45, 33, 53, 44, 59, 48],
[55, 54, 60, 53, 59, 57, 61],
[65, 56, 55, 52, 55, 62, 57],
];
const lowestWeeklyAverage = (weather) => {
let weeklyLows = [];
weather.forEach(week => {
weeklyLows.push(week.reduce((a, b) => a + b) / week.length);
});
return Math.min(...weeklyLows);
};
/* ------------------------------------------------------------------------------------------------
CHALLENGE 11 - Stretch Goal
Write a function called excel that accepts a string representing rows and columns in a table.
Rows are seperated by newline "\n" characters. Columns are seperated by commas. For example, '1,1,1\n4,4,4\n9,9,9' represents a 3x3 table.
The function should parse the string as rows and columns and compute the sum of the values for each row. Return an array with the sum of the values in each row.
For example, excel('1,1,1\n4,4,4\n9,9,9') returns [3, 12, 27].
------------------------------------------------------------------------------------------------ */
// const excel = (str) => {
// let sums = [];
// let rows = str.split('\n');
// rows.forEach(row => {
// let values = row.split(',').map(x => +x);
// sums.push(values.reduce((a,b) => a + b));
// });
// return sums;
// };
const excel = (str) => str.split('\n').map(rows => rows.split(',').map(x => +x).reduce((a,b) => a + b));
/* ------------------------------------------------------------------------------------------------
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-12.test.js
------------------------------------------------------------------------------------------------ */
describe('Testing challenge 1', () => {
test('it should return the last 10 characters of a string as an array', () => {
expect(returnTen('hello world')).toStrictEqual(['e','l','l','o',' ','w','o','r','l','d']);
expect(returnTen('world')).toStrictEqual(['w','o','r','l','d']);
});
});
describe('Testing challenge 2', () => {
test('It should return the max value', () => {
expect(findMax([[13,24,24,2], [2,5,6], [2,3]])).toStrictEqual(24);
});
});
describe('Testing challenge 3', () => {
test('It should return the total sum', () => {
expect(totalSum([[13,24,24,2], [2,5,6], [2,3]])).toStrictEqual(81);
expect(totalSum([])).toStrictEqual(0);
});
});
describe('Testing challenge 4', () => {
test('It should add the hourly totals array', () => {
expect(grandTotal(cookieStores)).toStrictEqual([88, 153, 252, 286, 139, 161, 145, 232, 276, 207, 161, 169]);
});
});
describe('Testing challenge 5', () => {
test('It should create an object of data for each store', () => {
expect(salesData(hoursOpen, grandTotal(cookieStores))).toStrictEqual([
{ sales: '88 cookies', time: '9 a.m.' },
{ sales: '153 cookies', time: '10 a.m.' },
{ sales: '252 cookies', time: '11 a.m.' },
{ sales: '286 cookies', time: '12 p.m.' },
{ sales: '139 cookies', time: '1 p.m.' },
{ sales: '161 cookies', time: '2 p.m.' },
{ sales: '145 cookies', time: '3 p.m.' },
{ sales: '232 cookies', time: '4 p.m.' },
{ sales: '276 cookies', time: '5 p.m.' },
{ sales: '207 cookies', time: '6 p.m.' },
{ sales: '161 cookies', time: '7 p.m.' },
{ sales: '169 cookies', time: '8 p.m.' }
]);
expect(salesData(hoursOpen, grandTotal(cookieStores)).length).toStrictEqual(hoursOpen.length);
});
});
describe('Testing challenge 6', () => {
test('It should return the number 24', () => {
expect(howManyTreats(errands)).toStrictEqual(24);
});
});
describe('Testing challenge 7', () => {
const battleshipData = [
['#', ' ', '#', ' '],
['#', ' ', '#', ' '],
['#', ' ', ' ', ' '],
[' ', ' ', '#', '#'],
];
test('It should return "hit" when it hits a boat', () => {
expect(battleship(battleshipData, 0, 0)).toStrictEqual('hit');
expect(battleship(battleshipData, 1, 0)).toStrictEqual('hit');
});
test('It should return "miss" when it doesn\'t hit a boat', () => {
expect(battleship(battleshipData, 0, 1)).toStrictEqual('miss');
expect(battleship(battleshipData, 3, 0)).toStrictEqual('miss');
});
});
describe('Testing challenge 8', () => {
test('It should multiply all the numbers together', () => {
expect(calculateProduct([[1, 2], [3, 4], [5, 6]])).toStrictEqual(720);
});
test('It should return zero if there are any zeroes in the data', () => {
expect(calculateProduct([[2, 3, 4, 6, 0], [4, 3, 7], [2, 4, 6]])).toStrictEqual(0);
});
test('It should work even if some of the arrays contain no numbers', () => {
expect(calculateProduct([[1, 2], [], [3, 4, 5]])).toStrictEqual(120);
});
});
describe('Testing challenge 9', () => {
test('It should calculate and return the average temperature of the data set', () => {
expect(averageDailyTemperature(weeklyTemperatures)).toStrictEqual(60.25);
});
});
describe('Testing challenge 10', () => {
test('It should return the lowest weekly average temperature within the data set', () => {
expect(lowestWeeklyAverage(weeklyTemperatures)).toStrictEqual(57);
expect(lowestWeeklyAverage(lowestWeeklyTemperatureData)).toStrictEqual(46);
});
});
describe('Testing challenge 11', () => {
test('It should return the total count for each row', () => {
let result = excel('1,1,1\n4,4,4\n9,9,9');
expect(result.length).toStrictEqual(3);
expect(result[0]).toStrictEqual(3);
expect(result[1]).toStrictEqual(12);
expect(result[2]).toStrictEqual(27);
});
});