Skip to content

Commit 0bcc475

Browse files
Chandrasekar-Gt2013anurag
authored andcommitted
Flatten array (hacktoberfest17#970)
1 parent 7e89cf0 commit 0bcc475

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

recursion/js/flattenArray.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Given a nested array the objective is to flatten it to a signle array in the same order it appaers
3+
Sample i/p : [1,2,[3,4,[5,6],7],8,9]
4+
Expected o/p : [1,2,3,4,5,6,7,8,9,]
5+
*/
6+
7+
var input = [1,2,[3,4,[5,6],7],8,9]
8+
var output = [];
9+
10+
function flattenArray(input, output){
11+
for(var i = 0; i < input.length; i++) {
12+
if(Array.isArray(input[i])) {
13+
flattenArray(input[i], output);
14+
} else {
15+
output.push(input[i]);
16+
}
17+
}
18+
};
19+
20+
flattenArray(input, output);
21+
console.log(output);

0 commit comments

Comments
 (0)