Understanding reduce

So I have never used reduce…not sure if I did the challenge for it our not. I sent some questions off for my edit function that I can not figure out…until then I figured I should understand this.

In the spirit of the season, and with everyone in a panic of the hurricane that seems to be headed right towards us (I live in Charleston, SC) this example of map uses the hurricanes names

Below I get true, false, false which makes me believe that name represents the elements in the array.

const hurricanes = ["florence", "ernesto", "helene"]
function getHurricane(name){
 if(name === "florence"){
   return true;
 }else{
   return false
 }
}
const charleston = hurricanesmap(getHurricane)
console.log(charleston)

If the above is true what represents the elements in a reduce? Below gives me 11, but is total representing the elements or num? is total just the accumulator?

const number = [1,3,4,2,1]
function sum(total, num){
  return total+num
}
const nums = number.reduce(number)
console.log(nums)

Reduce reduces all elements in an array into a single value. With respect to your questions:

Did I miss reduce in that code? Since I don’t see a use of reduce, I don’t see what relevance it has here.

The total isn’t doing anything at the moment on my end. Reduce, like map, and filter, consists of a higher-order function. Thus, it takes a function as a parameter. So, with your example you probably want:

const nums = number.reduce(sum)

Then it will store 11 as the variable name nums. So, 11 represents nums, and has reduced number to 11.

Reduce was not intended to be in with the map. I used the map to show that name used as argument looks like it represents the elements of the array. I was seeing if that was correct.

if that is correct then what do total and num represent? I dont give a value to num or total, yet the output is 11. So total, and num are getting their values from the array. So which variable do these array elements got to? Does total represent the array number, or nums?

You can thing about reduce as COMBINE. Reduce take 2 parameters (a, b) , a= “previous”,b= “current”. It take first two values and combine them into one, after take next “current” value and combine this value with “previous” (combined) and so on.
so your code will be

const number = [1, 3, 4, 2, 1]
let arraySum = number.reduce((a, b) => a + b);
// let arrayMulti = number.reduce((a, b) => a * b); //24
// let arrayDivi = number.reduce((a, b) => a / b);  //0.416666
console.log(arraySum)  //11

2 Likes

The first argument of the reduce callback function represents the accumulator, the 2nd argument represents the current element being iterated over, and if a third argument is used, it represents the index of the current element being iterated over. The reduce method also has a second parameter which represents the initial starting value of the accumulator. If not specified, then the first element of the array is the initial value of the accumulator, so the the 2nd array element is used in the first iteration. In your example of:

const number = [1,3,4,2,1]
function sum(total, num){
  return total+num
}
const nums = number.reduce(number)
console.log(nums)

no initial value is specified for the accumulator (total), so total starts as 1 and num starts as 3. When you return total + num, you are returning a new value for the accumulator (total). It would be like saying total += num, so during the 2nd iteration, total starts as 4 and num is 4 which returns 8.

2 Likes