-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatNestedArrays.js
More file actions
29 lines (25 loc) · 886 Bytes
/
flatNestedArrays.js
File metadata and controls
29 lines (25 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Scrimba mascot Pumpkin has won the grand prize at an international
cat show. Below are Pumpkin's scores from the judges, as well as all the
prizes he's won. In all the excitement of victory,
they've become a jumbled mess of nested arrays. Let's
help Pumpkin by sorting it out.
Write a function to flatten nested arrays of strings or
numbers into a single array. There's a method
for this, but pratice both doing it manually and using the method.
Example input: [1, [4,5], [4,7,6,4], 3, 5]
Example output: [1, 4, 5, 4, 7, 6, 4, 3, 5]
*/
function flatten(arr){
const newArr = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
newArr.push(...arr[i]);
} else {
newArr.push(arr[i]);
}
}
return newArr;
//return arr.map(item => item.isArray() ? ...item : item);
}
module.exports = flatten;