Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
328 lines (291 loc) · 6.83 KB
/
Copy pathtest.js
File metadata and controls
328 lines (291 loc) · 6.83 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
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
const test = () => {
const arr = [1, 2, 3, 4, 5];
const obj = {
name: "John",
age: 20,
city: "New York",
};
const obj1 = Object.keys(obj).map(key => obj[key]);
const obj2 = Object.values(obj).map(value => value * 2);
Object.keys(obj).forEach(key => {
console.log(key);
});
for (const [key, value] of Object.entries(obj)) {
console.log(key, value);
}
const arr1 = arr.filter(item => item % 2 === 0);
const arr2 = arr.map(item => item * 2);
arr.forEach(item => {
console.log(item);
});
for (const i = 0; i < arr.length; i++) {
console.log(arr[i]);
arr[i] = "update " + arr[i];
}
//delete first
arr.splice(1, 1);
//delete last
arr.splice(arr.length - 1, 1);
//prepend
arr.unshift("add");
//append
arr.push("add");
};
switch (key) {
case value:
break;
default:
break;
}
function columnTraverse(matrix) {
const rows = matrix.length;
const cols = matrix[0].length;
let row = rows - 1;
let col = cols - 1;
let direction = "up";
const output = [];
let index = 0;
while (index < rows * cols) {
output[index++] = matrix[row][col];
if (row % (rows - 1) === 0 && index !== 1) {
if (row === 0) {
if (direction == "up") {
col--;
} else {
row++;
}
direction = "down";
} else if (row === rows - 1) {
if (direction == "down") {
col--;
} else {
row--;
}
direction = "up";
}
} else {
switch (direction) {
case "up":
row--;
break;
case "down":
row++;
break;
}
}
}
return output;
}
function verticalTraverse(matrix) {
if (matrix.length === 0 || matrix[0].length === 0) {
return [];
}
const rows = matrix.length;
const cols = matrix[0].length;
const result = [];
let index = 0;
let row = rows - 1;
let col = cols - 1;
while (index < rows * cols) {
result[index++] = matrix[row][col];
if (row === 0) {
row = rows - 1;
col--;
} else {
row--;
}
}
return result;
}
const matrix1 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
const result = columnTraverse(matrix1);
const result2 = verticalTraverse(matrix1);
console.log(result.join(" "));
console.log(result2.join(" "));
var element = document.getElementsByClassName("test");
if (element) {
element[0].addEventListener("click", () => {
console.log("clicked");
});
element[0].innerHTML = "clicked";
element[0].style.color = "red";
}
function solution1(a) {
a = parseInt(a).toString();
var arr = a.split("");
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += parseInt(arr[i]);
}
return sum;
}
function solution2(a) {
var indexOfMinimum = -1;
var minimalSum = Number.MAX_VALUE;
for (var i = 0; i < a.length; i++) {
var sum = 0;
for (var j = 0; j < a.length; j++) {
sum += Math.abs(a[j] - a[i]);
}
if (sum < minimalSum) {
minimalSum = sum;
indexOfMinimum = i;
}
}
return a[indexOfMinimum];
}
const pyramid = n => {
const rows = [];
for (let i = 1; i <= n; i++) {
const spaces = " ".repeat(n - i);
const stars = "*".repeat(2 * i - 1);
rows.push(spaces + stars);
}
console.log(rows.join("\n"));
return rows;
};
pyramid(5);
test();
function transposeSeating(seating) {
let rows = seating.length;
let cols = rows > 0 ? seating[0].length : 0;
let transposed = [];
for (let i = 0; i < cols; ++i) {
transposed[i] = [];
for (let j = 0; j < rows; ++j) {
transposed[i][j] = seating[j][i];
}
}
return transposed;
}
let restaurantSeating = [
[10, 11, 12],
[20, 21, 22],
];
let transposedSeating = transposeSeating(restaurantSeating);
for (let row of transposedSeating) {
console.log(row.join(" "));
}
function reverseTransposeMatrix(matrix) {
let rows = matrix.length;
let cols = rows > 0 ? matrix[0].length : 0;
let row = cols - 1;
let col = rows - 1;
let result = [];
for (let i = 0; i < cols; i++) {
result[cols - i] = [];
for (let j = 0; j < rows; j++) {
result[cols - i][j] = matrix[j][i];
}
}
return result;
}
let matrix = [
[101, 102, 103, 104],
[201, 202, 203, 204],
[301, 302, 303, 304],
];
let transposedMatrix = reverseTransposeMatrix(matrix);
for (let row of transposedMatrix) {
if (row) {
console.log(row.join(" "));
}
}
function reflectOverSecondaryDiagonal(matrix) {
let size = matrix.length;
let newMatrix = [];
for (let i = 0; i < size; ++i) {
newMatrix[i] = [];
for (let j = 0; j < size; ++j) {
// TODO: Complete the code to obtain the reflected square matrix in newMatrix.
newMatrix[i][j] = matrix[size - 1 - j][size - 1 - i];
}
}
return newMatrix;
}
// Example square matrix to reflect over the secondary diagonal
let squareMatrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
// TODO: Call the function on squareMatrix and store the result in transformedMatrix.
let transformedMatrix = reflectOverSecondaryDiagonal(squareMatrix);
// Print the transformed matrix.
for (let row of transformedMatrix) {
if (row) {
console.log(row.join(" "));
}
}
class Dog {
constructor(name) {
this.name = name;
}
}
class Labrador extends Dog {
constructor(name, size) {
super(name);
this.size = size;
}
}
const labrador = new Labrador("Buddy", "large");
console.log(labrador.name, labrador.size);
// Generator function
function* conversation() {
const answer = yield "What is your name?";
yield `Hello, ${answer}!`;
}
const chat = conversation();
console.log(chat.next().value); // "What is your name?"
console.log(chat.next("Alex").value); // "Hello, Alex!"
const getList = ([x, ...y]) => [x, y];
console.log(getList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
// output: [1, [2, 3, 4, 5, 6, 7, 8, 9, 10]]
const name = [..."012234"];
console.log(name);
// output: ["0", "1", "2", "2", "3", "4"]
const unique = new Set(name);
console.log([...unique]);
// output: ["0", "1", "2", "3", "4"]
// Circular pagination from pivot_id: treat dblist as a ring that starts at the pivot
// and stops after one full pass (no repeated ids). Extra space is only the returned
// page (O(page_size)), not a rotated copy of the full list.
function get(dblist, pivot_id, page_size, page_number) {
const n = dblist.length;
if (page_size <= 0 || page_number < 0 || n === 0) {
return [];
}
let pivotIndex = -1;
for (let i = 0; i < n; i++) {
if (dblist[i] === pivot_id) {
pivotIndex = i;
break;
}
}
if (pivotIndex === -1) {
return [];
}
const startOffset = page_number * page_size;
if (startOffset >= n) {
return [];
}
const count = Math.min(page_size, n - startOffset);
const page = [];
for (let i = 0; i < count; i++) {
page.push(dblist[(pivotIndex + startOffset + i) % n]);
}
return page;
}
const dblist = [2, 23, 5, 42, 7, 9, 19];
console.log(get(dblist, 5, 2, 0)); // [5, 42]
console.log(get(dblist, 5, 4, 0)); // [5, 42, 7, 9]
console.log(get(dblist, 5, 2, 1)); // [7, 9]
console.log(get(dblist, 5, 2, 2)); // [19, 2]
console.log(get(dblist, 5, 3, 2)); // [23] (remainder of the cycle)
console.log(get(dblist, 5, 2, 3)); // [23]
console.log(get(dblist, 5, 2, 6)); // []
console.log(get(dblist, 5, 2, 1000)); // []