From 65ec33638e0262e1920c7c49efdd966c577f1e6b Mon Sep 17 00:00:00 2001 From: Saqib Saghir Date: Mon, 15 Oct 2018 11:53:12 +0300 Subject: [PATCH] add snippet --- snippets_archive/squareSum.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets_archive/squareSum.md diff --git a/snippets_archive/squareSum.md b/snippets_archive/squareSum.md new file mode 100644 index 000000000..3ae951334 --- /dev/null +++ b/snippets_archive/squareSum.md @@ -0,0 +1,15 @@ +### squareSum + +Squares each number passed into it and then sums the results together. + +Use `Array.prototype.reduce()` to iterate over numbers and to declare an accumulater. +Use `Math.pow()` to calculate power of each number and add all numbers into acculmulator. + +```js +const squareSum = (arr) => arr.reduce((squareSum, number) => + squareSum + Math.pow(number, 2), 0); +``` + +```js +squareSum([1, 2, 2]); // 9 +``` \ No newline at end of file