Created
January 7, 2019 15:57
-
-
Save paveleremin/3e06cfbcaf77281de48065c6837a3790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function equals(arr1, arr2) { | |
return JSON.stringify(arr1) === JSON.stringify(arr2); | |
} | |
function flatten(arr) { | |
return arr.reduce((result, toFlatten) => { | |
return result.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); | |
}, []); | |
} | |
const tests = [ | |
{ | |
it: [[1,2,[3]],4], | |
should: [1, 2, 3, 4] | |
}, | |
{ | |
it: [[1,0,2,[3]],4], | |
should: [1, 0, 2, 3, 4] | |
}, | |
{ | |
it: [[[1,[[0]],2,[3]],4]], | |
should: [1, 0, 2, 3, 4] | |
}, | |
{ | |
it: [1], | |
should: [1] | |
}, | |
{ | |
it: [], | |
should: [] | |
}, | |
] | |
for (let { it, should } of tests) { | |
console.log(equals(flatten(it), should)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment