Skip to content
Prev Previous commit
Next Next commit
step2-6 done!
  • Loading branch information
EliaYazdi committed Aug 2, 2019
commit 7738e895a9cdd3e48ef451b87e1e21f93895cdda
43 changes: 36 additions & 7 deletions Week3/homework/step2-6.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
'use strict';

const arr2d = [[1, 2], [3, 4], [5, 6]];
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
const arr2d = [
[1, 2],
[3, 4],
[5, 6]
];
const arr3d = [
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
];

function flattenArray2d(arr) {
// Replace this comment and the next line with your code
console.log(arr);
let flattenArr2 = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
flattenArr2.push(arr[i][j])
}
}
return flattenArr2;
}
console.log(flattenArray2d(arr2d));


function flattenArray3d(arr) {
// Replace this comment and the next line with your code
console.log(arr);
let flattenArr3 = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let l = 0; l < arr[i][j].length; l++) {

flattenArr3.push(arr[i][j][l]);
}
}

}
return flattenArr3;
}

console.log(flattenArray2d(arr2d)); // -> [1, 2, 3, 4, 5, 6]
Expand All @@ -20,4 +49,4 @@ console.log(flattenArray3d(arr3d)); // -> [1, 2, 3, 4, 5, 6, 7, 8]
module.exports = {
flattenArray2d,
flattenArray3d,
};
};