Help me understand following code


I tried taking different courses for JavaScript and the guy doesn’t really go in-depth explaining things. The code is as follows (I shortened the list)

     const inventors = [
      { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
      { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
    const totalYears = inventors.reduce((total, inventor) => {
      return total + (inventor.passed - inventor.year);
 }, 0);

Now I ofcourse took my time to understand that the first step the code is executed, it will just set 0 for the ‘total’ variable. My question is why? Could someone write this piece in a simplier version for me to understand it? I kinda think about it as something like a fallback value, but how does JavaScript know it should set it to ‘total’?

Someone must’ve had this question before, I just don’t know what to search for really.

Oh, and I am not taking the course any further, I will stick with FCC from now on, way better explanation of stuff…

Thank you for Your help!

it’s part of reduce specs, the second argument is the starting value of the accumulator

i suppose you are wondering regarding the mechanics behind the reduce method. Reduce is a method which can be executed over arrays. In the particular case, you call it on the inventors array. Reduce is meant to go over each element in the array, applying some actions on it and in the end return a value. The value it accumulates(named “total” in your case) is the first parameter of its callback function. When the callback function is followed by an argument, this value is what your “total” initial value is(in your case thats 0). If that value is ommited, for initial value we take the first element in the array(but this is out of this case context). The “inventor” parameter in your callback function represents the current element the callback is executed on. By the looks of it, the code sums up the life length of every inventor in the list(its birth year is subtracted by its death year and then added up to the “total” value)

let totalYears=0

for(let i=0; i<inventors.length; i++){
  totalYears+=inventors[i].passed - inventors[i].year

this is a for loop which would return the same result, behaving similar to how reduce runs. It goes thru each element in the array and sums up the total years

1 Like

Well, I researched it on W3Schools before writing this post but probably got a bit lost somewhere… After reading it now from MDN I understand it clearly. I feel a bit stupid given I could have just clicked the other link Google threw at me when searching for reduce() method as a whole. Ahem. Well thank you very much for a quick response :grin:

Thank you for explanation! I understand it now. Seems I just got a little bit dull while reading through documentation :pensive: I understood how it works I just didn’t realize the 0 stands for initial value. Well, I still have much to learn :sweat_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.