Logo

Programming-Idioms

History of Idiom 202 > diff from v7 to v8

Edit summary for version 8 by TravelingTechGuy:
[JS] Added JSFiddle

Version 7

2019-09-29, 11:08:19

Version 8

2019-09-29, 11:27:34

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)
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.
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
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Demo URL
https://jsfiddle.net/k2j5cyah/