From c0ef28a3629d3bbcb6a6bcbea58aa9d732e16ea1 Mon Sep 17 00:00:00 2001 From: Cody Loyd Date: Thu, 11 Apr 2019 10:20:29 -0500 Subject: [PATCH] add explanation and simpler solution to removeFromArray --- removeFromArray/removeFromArray.js | 36 +++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/removeFromArray/removeFromArray.js b/removeFromArray/removeFromArray.js index 3ce7003..8bf214e 100644 --- a/removeFromArray/removeFromArray.js +++ b/removeFromArray/removeFromArray.js @@ -1,6 +1,32 @@ -var removeFromArray = function(...args) { - const array = args[0] - return array.filter(val => !args.includes(val)) -} +// 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;