# Why after 'splice', the result is 10? Why not 5 + 1 = 6?

Tell us what’s happening:

``````
function sumOfTen(arr) {
// change code below this line
arr.splice(1,2);
// change code above this line
return arr.reduce((a, b) => a + b);
}

// do not change code below this line
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
``````

First parameter of splice takes in starting index. In this case 5. The second parameter takes how many items from the initial index it should remove. It means remove 5 and 1.

If you add up the rest of them, it’s 10.

1 Like

You need to store the new array resulting from arr.splice(1,2) in order to use reduce on it.

You can do:

arr = arr.splice(1,2) and then arr.reduce((a,b)=> a+b)

2 Likes

You don’t have to assign result of applying the [].splice() because it mutates the original array.
@tuanldhe140929 as a suggestion I advise you to compare Array.prototype.splice() and Array.prototype.slice()

1 Like

Sorry @Deylraytop, I misunderstood the question.
I thought that @tuanldhe140929 wanted the result to be 6, and he could obtain it with the array returned from arr.splice() that is the array containing the elements deleted from the original one ([5,1]). That’s why I suggested it.

1 Like

@Alessia no worries. We are here to help one another.

2 Likes

.splice() is evil.

My suggestion is to avoid it all together.

1 Like