Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140071

How to use JavaScript reduce() method to add ranges between an interval

$
0
0

The program below is used to add the four ranges with the ranges array and display the result 139.

(20 - 1) + (11930 - 11904) + (12020 - 11931) + (6 - 1) = 139

However, I cannot quite understand the use of the start parameter and the zero (0) in the code.

Help me understand how they are processed in the program.

Thanks

let ranges = [[1,20],[11904, 11930],[11931, 12020],[1,6]];
     console.log(ranges.reduce((start, [from, to]) =>{
       return start + (to - from);
     }, 0));

Viewing all articles
Browse latest Browse all 140071

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>