# Stuck on the .reduce() function

So there is a challenge about the Splice function that I dont really get, Ironically I understand the .splice part but not the .reduce() part of this code, mdn wasn’t really helpful either…

``````function sumOfTen(arr) {
const a = arr.splice(2,2)

return arr.reduce((a, b) => a + b);
}

console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
``````

From what I’ve read it applys the function a+b to every element of arr. since I defined a as arr.splice(2,2) it should be 1 and 5 so 6… how do I get to 10 in the end result and what do I need b for?

`a` is the starting element, and `b` is the current element.

`reduce` moves through the array applying the function to the first and current element, then the first element is set to the result of that, and it applies it to that and the next element and so on. so

``````[1,2,3,4,5,6]
a is 1, b is 2
1 + 2 is 3
a is 3, b is 3
3 + 3 is 6
a is 6, b is 4
6 + 4 is 10
a is 10, b is 5
10 + 5 is 15
a is 15, b is 6
15 + 6 is 21
Finished!
``````
1 Like