Skip to content

Commit

Permalink
Flatten array (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandrasekar-G authored and t2013anurag committed Oct 23, 2017
1 parent 7e89cf0 commit 0bcc475
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions recursion/js/flattenArray.js
@@ -0,0 +1,21 @@
/*
Given a nested array the objective is to flatten it to a signle array in the same order it appaers
Sample i/p : [1,2,[3,4,[5,6],7],8,9]
Expected o/p : [1,2,3,4,5,6,7,8,9,]
*/

var input = [1,2,[3,4,[5,6],7],8,9]
var output = [];

function flattenArray(input, output){
for(var i = 0; i < input.length; i++) {
if(Array.isArray(input[i])) {
flattenArray(input[i], output);
} else {
output.push(input[i]);
}
}
};

flattenArray(input, output);
console.log(output);

0 comments on commit 0bcc475

Please sign in to comment.