1

I am trying to remove even integers from an array. The function I wrote is still returning an index with an even number in it. I'm not sure why I'm getting that kind of behavior. Any thoughts?

let arr = [6, 3, 19, 43, 12, 66, 43];

const removeEvenValues = arr => {
  arr.forEach((num, i) => {
    if(num % 2 === 0) arr.splice(i, 1);
  });
};

removeEvenValues(arr); // arr becomes [3, 19, 43, 66, 43] but should become [3, 19, 43, 43]
1
  • when splicing an array, it changes the array ... you'll want to process the array from end to beginning instead Commented Nov 2, 2019 at 23:05

1 Answer 1

2

Don't splice an array while iterating over it - that'll change it in place. For example, if you start with an array [2, 4, 6], and on the first iteration, remove the item at index 0, the array will then be [4, 6], and you'll go onto the second iteration, where i is 1. You'll then remove the item at the 1st index, which is 6, resulting in the array becoming [4].

Every time an item gets removed, your current method forgets to re-check the new item that fell into the index of the removed item.

It's very confusing. Either use a for loop and start at the end of the array instead, or (even better) use .filter:

let arr = [6, 3, 19, 43, 12, 66, 43];

const removeEvenValues = arr => arr.filter(num => num % 2 !== 0);

console.log(removeEvenValues(arr)); 

If you have to mutate the existing array (which usually isn't a good idea unless absolutely necessary), then:

let arr = [6, 3, 19, 43, 12, 66, 43];

const removeEvenValues = arr => {
  for (let i = arr.length - 1; i >=0; i--) {
    if (arr[i] % 2 === 0) {
      arr.splice(i, 1);
    }
  }
  return arr;
}

console.log(removeEvenValues(arr));

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I should have mentioned that I needed to modify the original in place. I knew that there was an issue with the index shifting, but I couldn't quite conceptualize exactly what was happening. Thank you very much for the quick response. This helped me think about for loops in a new way, as well. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.