Stuck on "Removing Items Using Splice()"

I did this challenge a long time ago (I am a compSci student that was busy with other languages) and now I can’t find my solutions any more after the site has been redesigned. That is not cool!

Anyway, I am stuck on the “Removing Items Using Splice()” challenge. I did this:

function sumOfTen(arr) {
  // change code below this line
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] + arr[i+1] === 10) {
      arr.splice(arr[i+1], arr.length);
    }
  }
  // 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]));

Anyway, I am sure my splice call is wrong. I have been looking everywhere for the solution but I do not know what I am doing wrong. Any help is appreciated. Thanks!

Look at the examples they give.

And next time, can you link to the challenge?

Here is the link to the challenge. I am still stuck on it. Thanks!

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

You have to remove elements using splice so that when you use the reduce method in the challenge, it sums up all the elements in the array and the result is 10. No loop is needed, just remove stuff from array

I was a little stumped by this one too.

Were they looking for a general solution that would work on any array of numbers or a one off solution tailored to this particular array?

either one is fine, but general solutions are always better

Hello @saturnsbelt703,

Just for reference sake, the splice() function syntax takes an array and removes the elements in the array from the specified starting index to the desired number to be removed:

arr.splice(indexToStartRemovingFrom, howManyItemsShouldIRemove);
it can also take some optional arguments of “whatShouldIReplaceTheseItemsWith”. that is beyond the scope of the task.

To the task itself, use the splice function to remove index[1] and index[2]. the remaining elements would give you a sum of 10. see the solution below

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]));
5 Likes

Wow! The solution is a simple little baby line and I did a for loop and everything. It’s been a long week, overlooked this one!

1 Like

Why do we take two numbers started from the first ("arr.splice(1, 2)) ? It will be the number 5 and number 1 and the summ of these two numbers couldnt give us 10. Or I dont understand how reduce function works?
Thanks))

Hello @weregon7

As you know, indexes in javaScript start from 0. The given array has 6 values in it.
[2,5,1,5,2,1]

index[0] = 2;
index[1] = 5;
index[2] = 1;
index[3] = 5;
index[4] = 2;
index[5] = 1;

to get a sum of 10 from the array above, we need to remove 2 elements (5 and 1). As i mentioned in my earlier post, the splice function woks like this:
arr.splice(indexToStartRemovingFrom, howManyItemsShouldIRemove);

to remove “5 and 1” we start from 5, which is in index[1], and remove 2 elements (index[1] and index[2]) .

I hope this helps

2 Likes

thanks man
@chinonsoebere

I found a different solution than what others did. My solution involved using splice multiple times. First I use shift, then I used slice, and then I used slice again. The arr array was [5, 5] by the end of that process. I hadn’t observed that you could just remove one 5 and then remaining numbers would sum to 10.

Can someone please explain what this line means return arr.reduce((a, b) => a + b);?

check out the documentation on the reduce method

I already googled this dosen’t help me

what do you not understand of the documentation on the method?

When I started doing this question, the array changed to [2, 4, 5, 1, 7, 5, 2, 1].

I came to this for my solution:

// only change code below this line
let sum = 0;
for (let i = 0; i < arr.length; i++) {
  if (sum + arr[i] <= 10) {
    sum += arr[i];
  } else {
    arr.splice(i, 1, 0);
  }
}
// only change code above this line

I have been wrangling with a general solution to this challenge and couldn’t find an answer. After some time, I took inspiration from your code and implemented something pretty much identical.
The problem I have here is, that if we set the last item in arr to be 22, the sum will not be equal to 10…
After all we are only adding the numbers from left to right.

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.