Merge branch 'solutions' into master

This commit is contained in:
xandora
2021-05-09 00:10:19 +12:00
committed by GitHub
2939 changed files with 303672 additions and 173 deletions
+30 -3
View File
@@ -1,5 +1,32 @@
const removeFromArray = function() {
// 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
// is using the spread 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;
};
}
module.exports = removeFromArray
// 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]
// return array.filter(val => !args.includes(val))
// }
//
module.exports = removeFromArray;
+5 -5
View File
@@ -4,19 +4,19 @@ describe('removeFromArray', function() {
it('removes a single value', function() {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
xit('removes multiple values', function() {
it('removes multiple values', function() {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
xit('ignores non present values', function() {
it('ignores non present values', function() {
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
});
xit('ignores non present values, but still works', function() {
it('ignores non present values, but still works', function() {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
xit('can remove all values', function() {
it('can remove all values', function() {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
xit('works with strings', function() {
it('works with strings', function() {
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
});
xit('only removes same type', function() {