Created
January 29, 2019 23:06
-
-
Save jdmg94/469a291d0238df798e312cc443939755 to your computer and use it in GitHub Desktop.
A Technical test from CitrusByte
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
const flattenArray = (initialArray, depth = (1/0), result = []) => { | |
if(initialArray == null){ | |
return result; | |
} | |
for(const item of initialArray){ | |
if(depth > 1 && Array.isArray(item)){ | |
flattenArray(item, depth - 1, result); | |
} else { | |
result.push(item); | |
} | |
} | |
return result; | |
} | |
const input = [[1,2,[3]],4]; | |
console.log(flattenArray(input)); // should return [1,2,3,4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment