nas5w / javascript-tips-and-tidbits

A continuously-evolving compendium of javascript tips based on common areas of confusion or misunderstanding.
MIT License
1.2k stars 73 forks source link

Add initialValue to reduce method #15

Closed RinatValiullov closed 5 years ago

RinatValiullov commented 5 years ago

I think It will be better add initialValue in the section: Array Methods in reduce() method. In my opinion, it is better to clarify the internal logic of this method, isn't it?

nas5w commented 5 years ago

Definitely! Any thoughts on how the example should be clarified? Perhaps just a "Note" stating that an optional second parameter can be provided establishing an initial value?

RinatValiullov commented 5 years ago

I think @nas5w make more example codes with reduce() where user can see intent of optional initialValue

let arr = [1, 2, 3, 4, 5];

// removed initial value from reduce (no 0)
// If there’s no initial, then reduce takes the first element of the array as the initial value and starts the iteration from the 2nd element
console.log( result ); // 15
let result = arr.reduce((sum, current) => sum + current);

but

let arr = [];

// Error: Reduce of empty array with no initial value
// if the initial value existed, reduce would return it for the empty arr.
arr.reduce((sum, current) => sum + current);

// Uncaught TypeError: Reduce of empty array with no initial value

So it’s advised to always specify the initial value.

nas5w commented 5 years ago

Okay, what are your thoughts on this being the updated text:

reduce: accumulate values as specified in function

const arr = [1, 2, 3, 4, 5, 6];
const reduced = arr.reduce((total, current) => total + current, 0);
console.log(reduced);
// 21

Note: It is always advised to specify an initialValue or you could receive an error. For example:

const arr = [];
const reduced = arr.reduce((total, current) => total + current);
console.log(reduced);
// Uncaught TypeError: Reduce of empty array with no initial value
RinatValiullov commented 5 years ago

Okay, what are your thoughts on this being the updated text:

reduce: accumulate values as specified in function

const arr = [1, 2, 3, 4, 5, 6];
const reduced = arr.reduce((total, current) => total + current, 0);
console.log(reduced);
// 21

Note: It is always advised to specify an initialValue or you could receive an error. For example:

const arr = [];
const reduced = arr.reduce((total, current) => total + current);
console.log(reduced);
// Uncaught TypeError: Reduce of empty array with no initial value

Good and clear example. I think it will be great to add a comment also: If there’s no initialValue, then reduce takes the first element of the array as the initialValue and starts the iteration from the 2nd element

nas5w commented 5 years ago

Are you interested in doing the pull request to close this issue?

RinatValiullov commented 5 years ago

Are you interested in doing the pull request to close this issue?

Yeah, sure. I will do it, OK?

nas5w commented 5 years ago

Yes please!