Last active
December 6, 2016 04:12
-
-
Save stanleycyang/f15e70525cfc0cdacc593f3fb496f5e4 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(intArr, flatArr) { | |
var results = flatArr || []; | |
if (intArr && intArr.length > 0) { | |
intArr.forEach(function(value) { | |
if (typeof value === 'number') { | |
results.push(value); | |
} else if (value instanceof Array) { | |
flatten(value, results); | |
} | |
}); | |
} | |
return results; | |
}; | |
// Test cases | |
console.log(flatten([[1,2,[3]],4])) | |
console.log(flatten([[[1,2],[3]],4])) | |
console.log(flatten([[1,[2,[3]]],4])) | |
console.log(flatten([[1,2,[3]],[4,8,9]])) | |
console.log(flatten([[1,2,[3]],[4, [4,6]]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment