Files
30-seconds-of-code/snippets_archive/squareSum.md
Angelos Chalaris 8efdf68e3c Update squareSum.md
2018-10-15 12:17:08 +03:00

409 B

squareSum

Squares each number in an array and then sums the results together.

Use Array.prototype.reduce() to iterate over numbers and to declare an accumulator. Use Math.pow() to calculate power of each number and add all numbers into acculmulator.

const squareSum = (arr) => arr.reduce((squareSum, number) =>
  squareSum + Math.pow(number, 2), 0);
squareSum([1, 2, 2]); // 9