Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursion solution in Clear category for [old] Flatten a List by kurosawa4434
var flatList=array=>{
var result=[];
array.forEach(a=>{
if(Array.isArray(a)){
result = result.concat(flatList(a));
}else{
result.push(a);
}
});
return result;
};
var assert = require('assert');
if (!global.is_checking) {
assert.deepEqual(flatList([1, 2, 3]), [1, 2, 3], "First");
assert.equal(flatList([1, [2, 2, 2], 4]), [1, 2, 2, 2, 4], "Second");
assert.equal(flatList([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]), [2, 4, 5, 6, 6, 6, 6, 6, 7], "Third");
assert.equal(flatList([-1, [1, [-2], 1], -1]), [-1, 1, -2, 1, -1], "Four");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
May 15, 2017