Middle+
Task: implement a sumSquares function that sums the squares of numbers from an array using a custom reduce function. Only the sumSquares function can be modified.
sobes.tech AI
Answer from AI
The implementation of the sumSquares function using a custom reduce function might look like this:
function sumSquares(arr) {
return arr.reduce((acc, x) => acc + x * x, 0);
}
// Example usage:
console.log(sumSquares([1, 2, 3])); // 1 + 4 + 9 = 14
Here, reduce takes an accumulator acc and the current element x, accumulating the sum of the squares of the array elements. The initial value of the accumulator is 0.