13
snippets_archive/squareSum.md
Normal file
13
snippets_archive/squareSum.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
### squareSum
|
||||||
|
|
||||||
|
Squares each number in an array and then sums the results together.
|
||||||
|
|
||||||
|
Use `Array.prototype.reduce()` in combination with `Math.pow()` to iterate over numbers and sum their squares into an accumulator.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
squareSum(1, 2, 2); // 9
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user