Stuck on "Removing Items Using Splice()"

Stuck on "Removing Items Using Splice()"
0

#1

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!


#2

Look at the examples they give.

And next time, can you link to the challenge?


#3

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


#4

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


#5

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?


#6

either one is fine, but general solutions are always better


#7

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

#8

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!


#9

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))


#10

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


#11

thanks man
@chinonsoebere


#12

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.