odin-codespace/javascript/recursion/q8.js
2024-01-24 07:34:04 -05:00

27 lines
554 B
JavaScript

function SumSquares(arr) {
let sum = 0;
if (arr.length === 0) return 0;
let first = arr.shift();
if (Number.isInteger(first)) {
sum = first * first;
} else if (Array.isArray(first)) {
sum += SumSquares(first);
}
return (sum += SumSquares(arr));
}
var l = [1, 2, 3];
console.log(SumSquares(l)); // 1 + 4 + 9 = 14
l = [[1, 2], 3];
console.log(SumSquares(l)); // 1 + 4 + 9 = 14
l = [[[[[[[[[1]]]]]]]]];
console.log(SumSquares(l)); // 1 = 1
l = [10, [[10], 10], [10]];
console.log(SumSquares(l)); // 100 + 100 + 100 + 100 = 400