javascriptarrayssplice

remove elements from an array javascript


this the forth project from the odin project, all tests passed but the fifth one which required removing all elements failed and when i run the code it returns an array with half elements in original array before mutating.

I don't know why IT DOESN'T RETURN AN EMPTY ARRAY.in the fifth test.

const removeFromArray = function (array, ...deleteElement) {
    for (let i = 0; i < array.length; i++) {
        if (array.includes(deleteElement[i])) {
            array.splice(array.indexOf(deleteElement[i]), 1);
        }
    }
    return array;
};

const randomArray = [1, 2, 3, 4];
console.log(removeFromArray(randomArray, 1, 2, 3, 4));

and this the test

const removeFromArray = require('./removeFromArray')

describe('removeFromArray', () => {
  test('removes a single value', () => {
    expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
  });
  test('removes multiple values', () => {
    expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
  });
  test('ignores non present values', () => {
    expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
  });
  test('ignores non present values, but still works', () => {
    expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
  });
  test.skip('can remove all values', () => {
    expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
  });
  test.skip('works with strings', () => {
    expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
  });
  test.skip('only removes same type', () => {
    expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
  });
});

Solution

  • In the for you are looping trough the wrong element. It should be deleteElement.length, not array.length

    const removeFromArray = function(array, ...deleteElement) {
      for (let i = 0; i < deleteElement.length; i++) {
        if (array.includes(deleteElement[i])) {
          array.splice(array.indexOf(deleteElement[i]), 1);
        }
      }
      return array;
    };
    
    const randomArray = [1, 2, 3, 4];
    console.log(removeFromArray(randomArray, 1, 2, 3, 4));

    EDIT: although this will not work correctly when there are 2 same element to delete in the array. Here is another implementation which is deleting all matched indexes and not only the first one.

    const removeFromArray = function(array, ...deleteElement) {
      for (let i = 0; i < deleteElement.length; i++) {
        let foundIndex = array.indexOf(deleteElement[i]);
        while (foundIndex !== -1) {
          array.splice(foundIndex, 1);
          foundIndex = array.indexOf(deleteElement[i]);
        }
      }
      return array;
    };
    
    const randomArray = [1, 2, 3, 1, 4];
    console.log(removeFromArray(randomArray, 1));