-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.js
54 lines (43 loc) · 1.15 KB
/
flatten.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
const flatten = function (arr){
let newArray = [];
for ( let ele of arr)
{
if(Array.isArray(ele)){
let temp = ele;
for (let item of temp) {
newArray.push(item);
}
}
else {
newArray.push(ele);
}
}
return newArray;
}
function assertArraysEqual( arr1, arr2) {
let flag = false;
console.log(arr1);
console.log(arr2);
for (let i = 0;i < arr1.length; i++){
if (arr1[i] === arr2[i]) {
flag = true;
}
else {
flag = false;
}
}
if (flag) {
//console.log(`Assertion Passed: ${arr1} === ${arr2} `);
console.log("Assertion Passed:", arr1, "===", arr2 );
}
else {
//console.log(`Assertion Failed: ${arr1} === ${arr2} `);
console.log("Assertion Failed:", arr1, "===", arr2 );
}
}
//console.log(flatten([1, 2, [3, 4], 5, [6]]));
assertArraysEqual(([1, 2, [3, 4], 5, [6]]), flatten([1, 2, [3, 4], 5, [6]]));
//output:
// [1, 2, Array(2), 5, Array(1)]
// [1, 2, 3, 4, 5, 6]
// Assertion Failed: 1,2,3,4,5,6 === 1,2,3,4,5,6