Skip to content

Instantly share code, notes, and snippets.

@jdmg94
Created January 29, 2019 23:06
Show Gist options
  • Save jdmg94/469a291d0238df798e312cc443939755 to your computer and use it in GitHub Desktop.
Save jdmg94/469a291d0238df798e312cc443939755 to your computer and use it in GitHub Desktop.
A Technical test from CitrusByte
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