From 65ec33638e0262e1920c7c49efdd966c577f1e6b 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 fad13712d291305db6333225a490a11ea64405b4 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 212275414be00a1cfd12fa843a9bd1c988b8f8d3 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 2505974a3c241269bf8fd5f5a01590783f52af30 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);