Sum of squares
Solutions
Solution 1 (Click to Show/Hide)
function sumsq(array) {
let sum = 0;
for (let i = 0, len = array.length; i < len; i++) {
sum += array[i] * array[i];
}
return sum;
}
This solution uses a for
loop over the length of the array to compute the sum of squares.
Solution 2 (Click to Show/Hide)
return array.reduce((acc, curr) => acc + curr**2, 0);
}
This solution uses ES6 to simplify the code.