From 7c543b10fe67ebab32831bed40d76e87a9df4096 Mon Sep 17 00:00:00 2001 From: Mohammed Nabeel Date: Sat, 18 Jul 2020 13:46:10 +0300 Subject: [PATCH] All done. --- removeFromArray/removeFromArray.js | 18 ++++++++-- removeFromArray/removeFromArray.spec.js | 44 ++++++++++++------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/removeFromArray/removeFromArray.js b/removeFromArray/removeFromArray.js index 11efd5c..8ab5896 100644 --- a/removeFromArray/removeFromArray.js +++ b/removeFromArray/removeFromArray.js @@ -1,5 +1,19 @@ -const removeFromArray = function() { +const removeFromArray = function(...args) { + + let myArray = args[0]; //Create an array with the first function argument + let pos = 0; //Create a variable to store index of element + + // Remove all the remaining elements from the array + for (let i = 1; i < args.length; i++) { + pos = myArray.indexOf(args[i]); + + if (pos >= 0 && myArray[pos] === args[i]) { //Check argument and type + myArray.splice(pos, 1); + } + } + + return myArray; } -module.exports = removeFromArray +module.exports = removeFromArray \ No newline at end of file diff --git a/removeFromArray/removeFromArray.spec.js b/removeFromArray/removeFromArray.spec.js index f60239a..b280f00 100644 --- a/removeFromArray/removeFromArray.spec.js +++ b/removeFromArray/removeFromArray.spec.js @@ -1,25 +1,25 @@ const removeFromArray = require('./removeFromArray') describe('removeFromArray', function() { - it('removes a single value', function() { - expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]); - }); - xit('removes multiple values', function() { - expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]); - }); - xit('ignores non present values', function() { - expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]); - }); - xit('ignores non present values, but still works', function() { - expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]); - }); - xit('can remove all values', function() { - expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]); - }); - xit('works with strings', function() { - expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]); - }); - xit('only removes same type', function() { - expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); - }); -}); + 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"]); + }); + it('only removes same type', function() { + expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); + }); +}); \ No newline at end of file