Skip to content

Instantly share code, notes, and snippets.

View hbarudin's full-sized avatar

Hannah Barudin hbarudin

View GitHub Profile
@hbarudin
hbarudin / array_flatten.js
Last active May 2, 2017 13:21
Flatten an arbitrarily nested array (JS)
/*
Write some code, that will flatten an array of arbitrarily nested arrays of
integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
*/
var array_flatten = function (arr, flattened_arr = []) {
if(!Array.isArray(arr)) {
// Fow now, arbitrarily decide to just break out if input is invalid. Could handle other ways (return an empty array, for example), but this is OK for now.
console.error('Invalid input.');
return;