-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayInArray.js
More file actions
executable file
·33 lines (27 loc) · 919 Bytes
/
arrayInArray.js
File metadata and controls
executable file
·33 lines (27 loc) · 919 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
30
31
32
33
const bestTimes = [31, 1, 1, [3, 4, [8]], 44, [11]];
/**
* recur function: Accepts array or number
* if x[index] is an array, calls itself
* if x[index] addable, adds it in*/
let inNestSum = 0;
let recur = parcel =>{
for (let i = 0; i < parcel.length; i++){
// Is this member an array?
if (Array.isArray(parcel[i])){
recur(parcel[i]);
}
// Is this member a number?
if (typeof parcel[i] === 'number'){
inNestSum += parcel[i];
console.log(`Just added ${parcel[i]} to sum and got ${inNestSum} `)
}
}
return inNestSum;
}
console.log(recur(bestTimes));
/*
Jan 26, 2019 Evan Genest
Same problem from a month ago; see if you can remember the recursive base case.
This problem asks you to sum up all of the numbers within an array, but the array may also contains other arrays with numbers. This is what we call a nested array. For example:
[1, 1, 1, [3, 4, [8]], [5]]
*/