-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-tasks.js
More file actions
221 lines (185 loc) · 5.87 KB
/
array-tasks.js
File metadata and controls
221 lines (185 loc) · 5.87 KB
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
/* eslint-disable no-console */
// see https://javascript.info/array-methods#tasks
// Translate border-left-width to borderLeftWidth
function task1() {
function camelize(str) {
const result = str.split('-').map((item, index) => {
if (index !== 0 && item) {
return item[0].toUpperCase() + item.slice(1).toLowerCase();
}
return item.toLowerCase();
}).join('');
return result;
}
console.log(camelize('background------color'));
console.log(camelize('list-style-IMaGe'));
console.log(camelize('-----webkit-transition'));
}
// Filter range
function task2() {
function filterRange(arr, min, max) {
return arr.filter((item) => item >= min && item <= max);
}
const arr = [5, 3, 8, 1];
const filtered = filterRange(arr, 1, 4);
console.log(`filtered array: ${filtered}`); // 3,1 (matching values)
console.log(`original array: ${arr}`); // 5,3,8,1 (not modified)
}
// Filter range "in place"
function task3() {
function filterRangeInPlace(arr, min, max) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] < min || arr[i] > max) {
arr.splice(i, 1);
}
}
}
const arr = [5, 3, 8, 1];
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
console.log(`filtered in place: ${arr}`); // [3, 1]
}
// Sort in decreasing order
function task4() {
const arr = [5, 2, 10, -10, 8];
arr.sort((a, b) => b - a);
console.log(arr); // 10, 8, 5, 2, -10
}
// Copy and sort array
function task5() {
function copySorted(arr) {
// return Array.from(arr).sort();
return arr.slice().sort();
}
const arr = ['HTML', 'JavaScript', 'CSS'];
const sorted = copySorted(arr);
console.log(`sorted: ${sorted}`); // CSS, HTML, JavaScript
console.log(`original: ${arr}`); // HTML, JavaScript, CSS (no changes)
}
// TASK 6, Extendable calculator
// TODO: implement
// Map to names
function task7() {
const john = { name: 'John', age: 25 };
const pete = { name: 'Pete', age: 30 };
const mary = { name: 'Mary', age: 28 };
const users = [john, pete, mary];
const names = users.map((item) => item.name);
console.log(`names: ${names}`); // John, Pete, Mary
}
// Map to objects
function task8() {
const john = { name: 'John', surname: 'Smith', id: 1 };
const pete = { name: 'Pete', surname: 'Hunt', id: 2 };
const mary = { name: 'Mary', surname: 'Key', id: 3 };
const users = [john, pete, mary];
// note that we have to use an additional pair of braces ()
const usersMapped = users.map((item) => ({
fullName: `${item.name} ${item.surname}`,
id: item.id,
}));
/*
usersMapped = [
{ fullName: "John Smith", id: 1 },
{ fullName: "Pete Hunt", id: 2 },
{ fullName: "Mary Key", id: 3 }
]
*/
console.log(usersMapped);
}
// Sort users by age
function task9() {
function sortByAge(arr) {
return arr.sort((a, b) => a.age - b.age);
}
const john = { name: 'John', age: 25 };
const pete = { name: 'Pete', age: 30 };
const mary = { name: 'Mary', age: 28 };
const arr = [pete, john, mary];
sortByAge(arr);
console.log(arr[0].name); // John
console.log(arr[1].name); // Mary
console.log(arr[2].name); // Pete
}
// Shuffle an array
function task10() {
function shuffleSort(arr) {
// Because the sorting function is not meant to be used this way,
// not all permutations have the same probability.
return arr.sort(() => Math.random() - 0.5);
}
// using Fisher-Yates algorithm
function shuffleBetter(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const k = Math.floor(Math.random() * (i + 1));
// swap array elements using destructuring assignment syntax
[arr[i], arr[k]] = [arr[k], arr[i]];
}
return arr;
}
function shuffle(arr, shuffleFn) {
console.log(shuffleFn.name);
for (let i = 0; i < 5; i++) {
shuffleFn(arr);
console.log(arr);
}
}
shuffle([1, 2, 3], shuffleSort);
shuffle([1, 2, 3], shuffleBetter);
}
// Get average age
function task11() {
function getAverageAge(arr) {
return arr.reduce((sum, item) => sum + item.age, 0) / arr.length;
}
const john = { name: 'John', age: 25 };
const pete = { name: 'Pete', age: 30 };
const mary = { name: 'Mary', age: 29 };
const arr = [john, pete, mary];
console.log(getAverageAge(arr)); // (25 + 30 + 29) / 3 = 28
}
// Filter unique array members
function task12() {
function unique(arr) {
const newArr = [];
arr.forEach((element) => {
if (!newArr.includes(element)) {
newArr.push(element);
}
});
return newArr;
}
const strings = ['Hare', 'Krishna', 'Hare', 'Krishna',
'Krishna', 'Krishna', 'Hare', 'Hare', ':-O',
];
console.log(unique(strings)); // Hare, Krishna, :-O
}
// Create keyed object from array
function task13() {
function groupByIdIterate(arr) {
const usersById = {};
arr.forEach((element) => {
usersById[element.id] = element;
});
return usersById;
}
function groupByIdReduce(arr) {
return arr.reduce((usersById, current) => {
usersById[current.id] = current;
return usersById;
}, {});
}
const users = [
{ id: 'john', name: 'John Smith', age: 20 },
{ id: 'ann', name: 'Ann Smith', age: 24 },
{ id: 'pete', name: 'Pete Peterson', age: 31 },
];
/*
{
john: {id: 'john', name: "John Smith", age: 20},
ann: {id: 'ann', name: "Ann Smith", age: 24},
pete: {id: 'pete', name: "Pete Peterson", age: 31},
}
*/
console.log(groupByIdIterate(users));
console.log(groupByIdReduce(users));
}