|
Last change
on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
467 bytes
|
| Rev | Line | |
|---|
| [a762898] | 1 | function reduce(set, callback, initialValue) {
|
|---|
| 2 | if (initialValue == null && set.size === 0) {
|
|---|
| 3 | throw new TypeError('Reduce of empty set with no initial value');
|
|---|
| 4 | }
|
|---|
| 5 | let accumulator = initialValue;
|
|---|
| 6 | for (const value of set) {
|
|---|
| 7 | if (accumulator == null) {
|
|---|
| 8 | accumulator = value;
|
|---|
| 9 | }
|
|---|
| 10 | else {
|
|---|
| 11 | accumulator = callback(accumulator, value, value, set);
|
|---|
| 12 | }
|
|---|
| 13 | }
|
|---|
| 14 | return accumulator;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | export { reduce };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.