Removing from array (Fundamentals Part 4)


#1

I’m having trouble with the removeFromArray exercise in Fundamentals Part 4.
This is my code:
const removeFromArray = function(my_array) {
for (let i = 0; i < my_array.length; i++) {
for (let j = 0; j < arguments.length; j++) {
if (my_array[i] == arguments[j]) {
my_array.splice(i, 1);
}
}
}
return my_array;
}

module.exports = removeFromArray

code as an image because the forums doesn’t show indentation:
Screenshot_1

This is the spec.js file:
const removeFromArray = require(’./removeFromArray’)

describe(‘removeFromArray’, function() {
it(‘removes a single value’, function() {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
it(‘removes multiple values’, function() {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
it(‘ignores non present values’, function() {
expect(removeFromArray([1, 2, 3, 4], 7, “tacos”)).toEqual([1, 2, 3, 4]);
});
it(‘ignores non present values, but still works’, function() {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
it(‘can remove all values’, function() {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
it(‘works with strings’, function() {
expect(removeFromArray([“hey”, 2, 3, “ho”], “hey”, 3)).toEqual([2, “ho”]);
});
});

All tests pass except for the 2nd one which needs to equal [1, 4].
This is the error I get:
Failures:

  1. removeFromArray removes multiple values
    Message:
    Expected $.length = 3 to equal 2.
    Expected $[1] = 3 to equal 4.
    Unexpected $[2] = 4 in array.
    Stack:
    Error: Expected $.length = 3 to equal 2.
    Expected $[1] = 3 to equal 4.
    Unexpected $[2] = 4 in array.

#2

Hey @HalfGrape

You may find it easier to talk this through with someone in the discord chatrooms as these forums aren’t monitored as actively.