Write a completely new function that passes all tests
This commit is contained in:
parent
167042a03b
commit
9b1f10068a
|
@ -1,7 +1,28 @@
|
|||
const removeFromArray = function(arr, item) {
|
||||
let removedArr = arr.splice((item - 1), 1);
|
||||
return arr;
|
||||
//rest parameters! => ...Args
|
||||
|
||||
const removeFromArray = function(...input) {
|
||||
let array = input[0];
|
||||
let newArray = [];
|
||||
|
||||
array.forEach((item) => {
|
||||
if (!input.includes(item)) newArray.push(item);
|
||||
})
|
||||
|
||||
return newArray;
|
||||
};
|
||||
|
||||
//let's try the same but with .filter()!
|
||||
// const removeFromArray = function (...input) {
|
||||
// array = input[0];
|
||||
|
||||
// newArray = array.filter((item) => {
|
||||
// if (!input.includes(item)) return true;
|
||||
// })
|
||||
|
||||
// return newArray;
|
||||
// };
|
||||
|
||||
console.log(removeFromArray([1, 2, 3, 4], 2, 4));
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = removeFromArray;
|
||||
|
|
|
@ -4,22 +4,22 @@ describe('removeFromArray', () => {
|
|||
test('removes a single value', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||
});
|
||||
test.skip('removes multiple values', () => {
|
||||
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', () => {
|
||||
test('ignores non present values, but still works', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||
});
|
||||
test.skip('can remove all values', () => {
|
||||
test('can remove all values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||
});
|
||||
test.skip('works with strings', () => {
|
||||
test('works with strings', () => {
|
||||
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||
});
|
||||
test.skip('only removes same type', () => {
|
||||
test('only removes same type', () => {
|
||||
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue