-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.js
44 lines (37 loc) · 1.17 KB
/
day17.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
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]
*/
const kittyScores = [
[39, 99, 76], 89, 98, [87, 56, 90],
[96, 95], 40, 78, 50, [63]
];
const kittyPrizes = [
["💰", "🐟", "🐟"], "🏆", "💐", "💵", ["💵", "🏆"],
["🐟","💐", "💐"], "💵", "💵", ["🐟"], "🐟"
];
function flatten(arr){
const flattenedArray = [];
for (let i of arr) {
if (typeof i !== 'object') {
flattenedArray.push(i)
} else {
for (let j of i) {
if (typeof j !== 'object') {
flattenedArray.push(j)
}
}
}
}
return flattenedArray
}
console.log(flatten(kittyPrizes));
console.log(flatten(kittyScores));