From 2d6400b9bbf3b1d21c48f8907440e420972e8b2f Mon Sep 17 00:00:00 2001 From: NetMan <13informatyka14@gmail.com> Date: Fri, 5 Jan 2024 21:22:30 +0100 Subject: [PATCH] Passed the third test from exercise 04 --- 04_removeFromArray/removeFromArray.js | 16 +++++++++------- 04_removeFromArray/removeFromArray.spec.js | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/04_removeFromArray/removeFromArray.js b/04_removeFromArray/removeFromArray.js index f96b1a8..41637e4 100644 --- a/04_removeFromArray/removeFromArray.js +++ b/04_removeFromArray/removeFromArray.js @@ -1,14 +1,16 @@ const removeFromArray = function(array, ...remove) { remove.forEach(element => { let findIndexElement = array.findIndex(x => x == element); - let saveArray = []; - for (let i = 0; i < array.length - findIndexElement; i++) { - saveArray.push(array.pop()); + if (findIndexElement >= 0) { + let saveArray = []; + for (let i = 0; i < array.length - findIndexElement; i++) { + saveArray.push(array.pop()); + } + array.pop(); + for (let i = 0; i < saveArray.length; i++) { + array.push(saveArray[i]); + } } - array.pop(); - for (let i = 0; i < saveArray.length; i++) { - array.push(saveArray[i]); - } }); return array; }; diff --git a/04_removeFromArray/removeFromArray.spec.js b/04_removeFromArray/removeFromArray.spec.js index 2d0797d..6c26e82 100644 --- a/04_removeFromArray/removeFromArray.spec.js +++ b/04_removeFromArray/removeFromArray.spec.js @@ -7,7 +7,7 @@ describe('removeFromArray', () => { test('removes multiple values', () => { expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]); }); - test.skip('ignores non present values', () => { + test('ignores non present values', () => { expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]); }); test.skip('ignores non present values, but still works', () => {