Logo

Programming-Idioms

History of Idiom 202 > diff from v6 to v7

Edit summary for version 7 by TravelingTechGuy:
New JS implementation by user [TravelingTechGuy]

Version 6

2019-09-29, 10:40:05

Version 7

2019-09-29, 11:08:19

Idiom #202 Sum of squares

Calculate the sum of squares s of data, an array of floating point values.

Idiom #202 Sum of squares

Calculate the sum of squares s of data, an array of floating point values.

Code
s = data.reduce((a, c) => a + c ** 2, 0)
Comments bubble
Array.reduce uses a function to reduce the array intoto a single sum of all the elements' squares.

The initial accumulator's value is 0.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce