Why does the second assignment need return and the first returns variable total automatically?
const total = inventors.reduce(((total, inventor) => total += inventor.passed - inventor.born), 0);
const transportation = data.reduce(((prevVal, currentVal) => {
if (!prevVal[currentVal]) {
prevVal[currentVal] = 0;
}
prevVal[currentVal]++;
return prevVal;
}), {})
If return prevVal is omitted I get Uncaught TypeError: Cannot read properties of undefined
What is the difference in how reduce() acts with values. Is it effected by ternary operation? If so then why this returns 0?
const transportation = data.reduce(((prevVal, currentVal) =>
(!prevVal[currentVal]) ? prevVal[currentVal] = 0 : prevVal[currentVal]++), {})