# Basic Data Structures: Remove Items Using splice() . --?

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/remove-items-using-splice

So what s a and b in the reduce() function? I have o idea how they are getting 16.
I understand that it is a function passing a and b and adding β¦ but what is a and b?

Hello
a : total Required. The initialValue , or the previously returned value of the function
b : currentValue Required. The value of the current element
So first a is 0 and b is arr[0]
Then a = 0 + arr[0] and b = arr[1]
After a = arr[0] + arr[1] and b = arr[2]
Until the end , the total will be accumulated in a.

i still dont get itβ¦ why a and b? i dont understand there placement or what it means.

a + b could mean anything. a + b is only two values how does that function total up to 16? it make no sence at all.

Hi ainneo, hereβs the mdn documentation on reduce.

The exercise isnβt focused on the reduce function. However if you are curious about reduce, you can read about it in detail there.

Here is also a great resource:

1 Like

Hi @ainneo,

It totals up to 16 because of this part of the problem:

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

When you call sumOfTen, you pass in the array: [2, 5, 1, 5, 2, 1]. If you add each of these #s up, it equals 16.

So how does it work? The βaβ is the accumulator and βbβ is the βcurrent valueβ of the array. Think of them like variables. You can actually use other variable names, but the project uses βaβ and βbβ.

Anyways, using the Array above: [2, 5, 1, 5, 2, 1], keep note of the a + b in `(a, b) => a + b`. The a + b returns a value to store in βaβ for the next step.

So at the beginning, βaβ = a + b, and βbβ = 2. Again, βbβ keeps track of the current value of the array. Since βaβ, at the beginning, has no initial value, βaβ will simply = βbβ, in other words βaβ = 2.

On the next step, current value in the array is 5 -> so now βbβ = 5. Well, we know βaβ from last time is = 2

• a + b = 2 + 5 = 7.

• This 7 is then stored in βaβ for the next step.

On the next step, the current value in the array is now 1. so βbβ = 1. We know βaβ from last step is = 7.

• a + b = 7 + 1 = 8.

• The 8 is then stored in βaβ for the next step.

On the next step, the current value in the array is now 5 --> βbβ = 5. βaβ from previous step = 8

• a + b = 8 + 5 = 13.

• The 13 is then stored in βaβ for the next step.

On the next step, the current value is now 2 --> βbβ = 2. βaβ from previous step = 13.

• a + b = 2 + 13 = 15.

• The 15 is then stored in βaβ for the next step.

Finally, on the last value of the Array, βbβ = 1. βaβ from the previous step = 15.

• a + b = 15 + 1 = 16

Since we reached the end of the [2, 5, 1, 5, 2, 1] array, the .reduce returns the final value in βaβ, which is 16.

The goal of the project is to splice the Array so that the returned value is 10, rather than 16. So your goal is to get the [2, 5, 1, 5, 2, 1] Array to look like [2, 5, 2, 1] using the .splice method since 2 + 5 + 2 + 1 = 10

2 Likes

a and b are variable there names are not important.
Example: [1, 2 ,3].reduce((x, y) => x + y)
return 6

1 Like