From 6dc2ec4003fda8c8d5292e92abcd41c5e5c5f6d1 Mon Sep 17 00:00:00 2001 From: Marlena Compton Date: Sat, 12 Nov 2016 16:46:46 -0800 Subject: [PATCH 1/2] tweak the reduce exercise for beginner friendliness --- exercises/basic_reduce/problem.md | 18 ++++++++++++++++++ exercises/basic_reduce/solution/solution.js | 8 ++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/exercises/basic_reduce/problem.md b/exercises/basic_reduce/problem.md index f1b7278..97796d4 100644 --- a/exercises/basic_reduce/problem.md +++ b/exercises/basic_reduce/problem.md @@ -40,3 +40,21 @@ function countWords(inputWords) { module.exports = countWords ``` + +Helps & Hints: +The main idea of a reduce function is that you are reducing the values in an array down to a single value. + +This single value is often called the accumulator because it is inside of this variable that values are accumulated. + +When you use the javascript array method for reduce, many of the examples have a one return for this method, but it can be easier +to return the accumulator after performing your work. + +```js +var sum = [0, 1, 2, 3].reduce(function(accumulator, current_value) { + + accumulator = accumulator + current_value + + return accumulator; +}, 0); +// sum is 6 +``` \ No newline at end of file diff --git a/exercises/basic_reduce/solution/solution.js b/exercises/basic_reduce/solution/solution.js index 13ebddf..ac90014 100644 --- a/exercises/basic_reduce/solution/solution.js +++ b/exercises/basic_reduce/solution/solution.js @@ -1,8 +1,8 @@ function countWords(arr) { - return arr.reduce(function(countMap, word) { - countMap[word] = ++countMap[word] || 1 // increment or initialize to 1 - return countMap - }, {}) // second argument to reduce initialises countMap to {} + return arr.reduce(function(accumulator, current_word) { + accumulator[current_word] = ++accumulator[current_word] || 1 // increment or initialize to 1 + return accumulator + }, {}) // second argument to reduce initialises accumulator to {} } module.exports = countWords From 6f07e01d9a853c158e8380c3fdb2ecdc8590350f Mon Sep 17 00:00:00 2001 From: Marlena Compton Date: Tue, 15 Nov 2016 22:03:12 -0600 Subject: [PATCH 2/2] remove semicolons, remove paragraph in the hints, change variable names back to original --- exercises/basic_reduce/problem.md | 10 ++++------ exercises/basic_reduce/solution/solution.js | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/exercises/basic_reduce/problem.md b/exercises/basic_reduce/problem.md index 97796d4..79f180f 100644 --- a/exercises/basic_reduce/problem.md +++ b/exercises/basic_reduce/problem.md @@ -44,17 +44,15 @@ module.exports = countWords Helps & Hints: The main idea of a reduce function is that you are reducing the values in an array down to a single value. -This single value is often called the accumulator because it is inside of this variable that values are accumulated. - -When you use the javascript array method for reduce, many of the examples have a one return for this method, but it can be easier -to return the accumulator after performing your work. +When you use the javascript array method for reduce, many of the examples have a one line return for this method, but it can be easier +to return the result after performing your work. ```js var sum = [0, 1, 2, 3].reduce(function(accumulator, current_value) { accumulator = accumulator + current_value - return accumulator; -}, 0); + return accumulator +}, 0) // sum is 6 ``` \ No newline at end of file diff --git a/exercises/basic_reduce/solution/solution.js b/exercises/basic_reduce/solution/solution.js index ac90014..13ebddf 100644 --- a/exercises/basic_reduce/solution/solution.js +++ b/exercises/basic_reduce/solution/solution.js @@ -1,8 +1,8 @@ function countWords(arr) { - return arr.reduce(function(accumulator, current_word) { - accumulator[current_word] = ++accumulator[current_word] || 1 // increment or initialize to 1 - return accumulator - }, {}) // second argument to reduce initialises accumulator to {} + return arr.reduce(function(countMap, word) { + countMap[word] = ++countMap[word] || 1 // increment or initialize to 1 + return countMap + }, {}) // second argument to reduce initialises countMap to {} } module.exports = countWords