feat: recursion

This commit is contained in:
Smigz 2024-01-24 07:34:04 -05:00
parent 5e71ed17da
commit ac9bd268d5
5 changed files with 161 additions and 0 deletions

View 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);