question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Condense arrays with reduce: Better variable naming

See original GitHub issue

Challenge 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:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
erictleungcommented, Jun 24, 2016

@sahilkhurana19 @raisedadead how about this?

… To use reduce you pass in a callback whose arguments are an accumulator (in this case, previousVal) and the current value (currentVal).

The accumulator is like a total that reduce keeps track of after each operation. The current value is just the next element in the array you’re iterating through. …

1reaction
sahilkhurana19commented, Jun 20, 2016

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found