mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
Update files
This commit is contained in:
@@ -1,29 +1,26 @@
|
||||
// we have 2 solutions here, an easier one and a more advanced one.
|
||||
// The easiest way to get an array of all of the arguments that are passed to a function
|
||||
// The easiest way to get an array of the rest of the arguments that are passed to a function
|
||||
// is using the rest operator. If this is unfamiliar to you look it up!
|
||||
const removeFromArray = function (...args) {
|
||||
// the very first item in our list of arguments is the array, we pull it out with args[0]
|
||||
const array = args[0];
|
||||
// create a new empty array
|
||||
const newArray = [];
|
||||
// use forEach to go through the array
|
||||
array.forEach((item) => {
|
||||
// push every element into the new array
|
||||
// UNLESS it is included in the function arguments
|
||||
// so we create a new array with every item, except those that should be removed
|
||||
if (!args.includes(item)) {
|
||||
newArray.push(item);
|
||||
}
|
||||
});
|
||||
// and return that array
|
||||
return newArray;
|
||||
const removeFromArray = function (array, ...args) {
|
||||
// create a new empty array
|
||||
const newArray = [];
|
||||
// use forEach to go through the array
|
||||
array.forEach((item) => {
|
||||
// push every element into the new array
|
||||
// UNLESS it is included in the function arguments
|
||||
// so we create a new array with every item, except those that should be removed
|
||||
if (!args.includes(item)) {
|
||||
newArray.push(item);
|
||||
}
|
||||
});
|
||||
// and return that array
|
||||
return newArray;
|
||||
};
|
||||
|
||||
// A simpler, but more advanced way to do it is to use the 'filter' function,
|
||||
// which basically does what we did with the forEach above.
|
||||
|
||||
// var removeFromArray = function(...args) {
|
||||
// const array = args[0]
|
||||
// var removeFromArray = function(array, ...args) {
|
||||
// return array.filter(val => !args.includes(val))
|
||||
// }
|
||||
//
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
const removeFromArray = require('./removeFromArray-solution');
|
||||
const removeFromArray = require("./removeFromArray");
|
||||
|
||||
describe('removeFromArray', () => {
|
||||
test('removes a single value', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||
});
|
||||
test.skip('removes multiple values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||
});
|
||||
test.skip('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', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||
});
|
||||
test.skip('can remove all values', () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||
});
|
||||
test.skip('works with strings', () => {
|
||||
expect(removeFromArray(['hey', 2, 3, 'ho'], 'hey', 3)).toEqual([
|
||||
2,
|
||||
'ho',
|
||||
]);
|
||||
});
|
||||
test.skip('only removes same type', () => {
|
||||
expect(removeFromArray([1, 2, 3], '1', 3)).toEqual([1, 2]);
|
||||
});
|
||||
describe("removeFromArray", () => {
|
||||
test("removes a single value", () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||
});
|
||||
test.skip("removes multiple values", () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||
});
|
||||
test.skip("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", () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||
});
|
||||
test.skip("can remove all values", () => {
|
||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||
});
|
||||
test.skip("works with strings", () => {
|
||||
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||
});
|
||||
test.skip("only removes same type", () => {
|
||||
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user