This commit is contained in:
Mohammed Nabeel 2020-07-15 18:53:58 +03:00
parent 67402e5235
commit 1987591d9f
2 changed files with 4 additions and 4 deletions

View File

@ -7,7 +7,7 @@ const removeFromArray = function(...args) {
// Remove all the remaining elements from the array // Remove all the remaining elements from the array
for (let i = 1; i < args.length; i++) { for (let i = 1; i < args.length; i++) {
pos = myArray.indexOf(args[i]); pos = myArray.indexOf(args[i]);
if (pos >= 0) { //Is it a valid argument if (pos >= 0 && myArray[pos] === args[i]) { //Is it a valid argument
myArray.splice(pos, 1); myArray.splice(pos, 1);
} }
} }

View File

@ -13,13 +13,13 @@ describe('removeFromArray', function() {
it('ignores non present values, but still works', function() { it('ignores non present values, but still works', function() {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]); expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
}); });
xit('can remove all values', function() { it('can remove all values', function() {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]); expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
}); });
xit('works with strings', function() { it('works with strings', function() {
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]); expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
}); });
xit('only removes same type', function() { it('only removes same type', function() {
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
}); });
}); });