mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-28 04:45:36 -04:00
feat: recursion
This commit is contained in:
parent
5e71ed17da
commit
ac9bd268d5
5 changed files with 161 additions and 0 deletions
20
javascript/recursion/q7.js
Normal file
20
javascript/recursion/q7.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
let seven = totalIntegers([[[5], 3], 0, 2, ["foo"], [], [4, [5, 6]]]); // 7
|
||||
|
||||
function totalIntegers(arr) {
|
||||
let total = 0;
|
||||
|
||||
if (arr.length === 0) return 0;
|
||||
|
||||
if (typeof arr !== "number") {
|
||||
let numb = arr.shift();
|
||||
|
||||
if (typeof numb === "number") {
|
||||
total += 1;
|
||||
} else if (Array.isArray(numb)) {
|
||||
total += totalIntegers(numb);
|
||||
}
|
||||
}
|
||||
return (total += totalIntegers(arr));
|
||||
}
|
||||
|
||||
console.log(seven);
|
Loading…
Add table
Add a link
Reference in a new issue