8000 Completed exercise #4 removeFromArray · rdansie/javascript-exercises@e69f1f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e69f1f0

Browse files
committed
Completed exercise TheOdinProject#4 removeFromArray
1 parent 57f6a18 commit e69f1f0

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

04_removeFromArray/removeFromArray.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
const removeFromArray = function(array, num) {
2-
//loop through elements of Array
3-
for (element of array) {
4-
//if it matches the function argument, delete it from the array
5-
if (element === num) {
6 8000 -
let index = array.indexOf(element);
7-
array.splice(index,1);
1+
const removeFromArray = function(array) {
2+
//Take arguments without the first array
3+
const newArg = [].slice.call(arguments, 1);
4+
//Iterate through elements of array
5+
for (let i = array.length - 1; i >= 0; i--) {
6+
//Iterate through elements of arguments
7+
for (let j = newArg.length; j >= 0; j--) {
8+
//If element matches argument then remove it from the array
9+
if (array[i] === newArg[j]) {
10+
array.splice(i,1);
11+
}
812
}
913
}
1014
return array;
15+
1116
};
1217

18+
removeFromArray([1,2,3,4],2,3);
19+
1320
// Do not edit below this line
1421
module.exports = removeFromArray;
15-
16-
17-
//turn arguments into array using Array.from()
18-
19-
20-
//then run previous function through all elements of arguments array except the 1st

04_removeFromArray/removeFromArray.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ describe('removeFromArray', () => {
77
test('removes multiple values', () => {
88
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
99
});
10-
test.skip('ignores non present values', () => {
10+
test('ignores non present values', () => {
1111
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
1212
});
13-
test.skip('ignores non present values, but still works', () => {
13+
test('ignores non present values, but still works', () => {
1414
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
1515
});
16-
test.skip('can remove all values', () => {
16+
test('can remove all values', () => {
1717
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
1818
});
19-
test.skip('works with strings', () => {
19+
test('works with strings', () => {
2020
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
2121
});
22-
test.skip('only removes same type', () => {
22+
test('only removes same type', () => {
2323
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
2424
});
2525
});

0 commit comments

Comments
 (0)
0