From e5b0c88297c92b5929313bb3284a0b19f79b145e Mon Sep 17 00:00:00 2001 From: ayer21 <98123280+ayer21@users.noreply.github.com> Date: Fri, 22 Apr 2022 02:37:14 +0000 Subject: [PATCH] New simple solution proposed this solution is based on two nested loops, one that takes an item from the first array, and one that checks if it's equal to a value in the second array, by looping through the second array. it also uses the splice method to remove the item from the array. so no need to create a new one. --- removeFromArray/removeFromArray.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/removeFromArray/removeFromArray.js b/removeFromArray/removeFromArray.js index 3a68b8a..6b3b2fa 100644 --- a/removeFromArray/removeFromArray.js +++ b/removeFromArray/removeFromArray.js @@ -28,4 +28,18 @@ const removeFromArray = function (...args) { // } // +// another solution is to loop through the first array items, and for each item, you check if it's on the second array. if true, +// the item is removed from the array using splice() method. +const removeFromArray = function (array, ...num) { + let i = 0; // this will be a counter for loop iterations, it will be used as the array's index indicator + firstArray: + while (i < array.length) { + for (item of num) { + if (array[i] === item) { array.splice(i, 1); continue firstArray } + } + i++; + } + return array; +}; + module.exports = removeFromArray;