Passed the third test from exercise 04

This commit is contained in:
NetMan 2024-01-05 21:22:30 +01:00
parent 553279a58a
commit 2d6400b9bb
2 changed files with 10 additions and 8 deletions

View File

@ -1,14 +1,16 @@
const removeFromArray = function(array, ...remove) { const removeFromArray = function(array, ...remove) {
remove.forEach(element => { remove.forEach(element => {
let findIndexElement = array.findIndex(x => x == element); let findIndexElement = array.findIndex(x => x == element);
let saveArray = []; if (findIndexElement >= 0) {
for (let i = 0; i < array.length - findIndexElement; i++) { let saveArray = [];
saveArray.push(array.pop()); 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; return array;
}; };

View File

@ -7,7 +7,7 @@ describe('removeFromArray', () => {
test('removes multiple values', () => { test('removes multiple values', () => {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]); 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]); expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
}); });
test.skip('ignores non present values, but still works', () => { test.skip('ignores non present values, but still works', () => {