Condense arrays with reduce: Better variable naming
See original GitHub issueChallenge Condense arrays with reduce has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
This is not quite a issue, but i think that if the variable “previousVal” is changed to “total” or “sum” would help others better understand and avoid confusion. As in this case previousVal represents an accumulator.
My code:
var array = [4,5,6,7,8];
var singleVal = 0;
// Only change code below this line.
singleVal = array.reduce(function(total, currentVal){
return total + currentVal; // previousVal is the accumulator
});
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Five Interesting Ways to Use Array.reduce() (And One Boring ...
reduce () method is a bit more flexible. It can return anything. Its purpose is to take an array and condense its content...
Read more >Array.prototype.reduce() - JavaScript - MDN Web Docs
The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value ...
Read more >How to simplify your codebase with map(), reduce(), and filter ...
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
Read more >9: Array .reduce() Method | JavaScript Array Methods - YouTube
The JavaScript Array reduce () method executes a reducer function on each element of the array and returns a single output value.
Read more >JavaScript Array Methods: how to use map and reduce
Methods are property names that we can assign to a functions. They can be invoked at any time just by using the name...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sahilkhurana19 @raisedadead how about this?
@erictleung So I guess it would be better if I reverted back the changes in my PR and add something like
In the following example reduce is being used to subtract all the values of an array, note that while previousVal is the accumulator, currentVal on the other hand iterates over the array and modifies the previousVal as per the return statement.