Created
November 26, 2018 15:17
-
-
Save scottlepp/32ea0585c47ffbfbcd128f7fcc15e5ed 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 flatten(data) { | |
return data.reduce(( acc, current ) => { | |
if (Array.isArray(current)) { | |
return acc.concat(flatten(current)); | |
} else { | |
return acc.concat(current); | |
} | |
}, []) | |
} | |
const test = [[1,2,[3]],4]; | |
const actual = flatten(test); | |
// Test Assertions | |
// Flattened length should equal 4 | |
if (actual.length !== 4) { | |
console.error('Failed: Length Not Eq 4'); | |
} | |
// Flattened values should not contain array | |
for (const val of actual) { | |
if (Array.isArray(val)) { | |
console.error('Failed: Value is Array'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment