forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstep2-6.js
More file actions
35 lines (30 loc) · 825 Bytes
/
step2-6.js
File metadata and controls
35 lines (30 loc) · 825 Bytes
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
'use strict';
const arr2d = [[1, 2], [3, 4], [5, 6]];
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
function flattenArray2d(arr) {
const newArray = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
newArray.push(arr[i][j]);
}
}
return newArray.sort();
}
function flattenArray3d(arr) {
const arr3dim = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let k = 0; k < arr[i][j].length; k++) {
arr3dim.push(arr[i][j][k]);
}
}
}
return arr3dim;
}
console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]
console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8]
// Do not change or remove anything below this line
module.exports = {
flattenArray2d,
flattenArray3d,
};