From 386cde09d1018af752903f5ea04e91d61f432615 Mon Sep 17 00:00:00 2001 From: Saqib Saghir Date: Mon, 15 Oct 2018 11:53:12 +0300 Subject: [PATCH 1/4] 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 From 8efdf68e3c3a6db3ab0dd9386b077c5b00f5dfc4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 15 Oct 2018 12:17:08 +0300 Subject: [PATCH 2/4] Update squareSum.md --- snippets_archive/squareSum.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets_archive/squareSum.md b/snippets_archive/squareSum.md index 3ae951334..f67f7689d 100644 --- a/snippets_archive/squareSum.md +++ b/snippets_archive/squareSum.md @@ -1,15 +1,15 @@ ### squareSum -Squares each number passed into it and then sums the results together. +Squares each number in an array and then sums the results together. -Use `Array.prototype.reduce()` to iterate over numbers and to declare an accumulater. +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. ```js const squareSum = (arr) => arr.reduce((squareSum, number) => - squareSum + Math.pow(number, 2), 0); + squareSum + Math.pow(number, 2), 0); ``` ```js squareSum([1, 2, 2]); // 9 -``` \ No newline at end of file +``` From 5bcd0980815445df8b79880ef370d147d885b06c Mon Sep 17 00:00:00 2001 From: Saqib Saghir Date: Tue, 16 Oct 2018 12:54:26 +0300 Subject: [PATCH 3/4] chore: minor updat --- snippets_archive/squareSum.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets_archive/squareSum.md b/snippets_archive/squareSum.md index 3ae951334..b130d5d35 100644 --- a/snippets_archive/squareSum.md +++ b/snippets_archive/squareSum.md @@ -6,10 +6,9 @@ Use `Array.prototype.reduce()` to iterate over numbers and to declare an accumul 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); +const squareSum = (...arr) => arr.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0); ``` ```js -squareSum([1, 2, 2]); // 9 +squareSum(1, 2, 2); // 9 ``` \ No newline at end of file From ade6be6cd7ecef2a4277655ba40c01e7e2091629 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 16 Oct 2018 13:55:06 +0300 Subject: [PATCH 4/4] Update squareSum.md --- snippets_archive/squareSum.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets_archive/squareSum.md b/snippets_archive/squareSum.md index b56f180ed..326504b40 100644 --- a/snippets_archive/squareSum.md +++ b/snippets_archive/squareSum.md @@ -2,8 +2,7 @@ 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. +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);