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.
This commit is contained in:
ayer21 2022-04-22 02:37:14 +00:00 committed by GitHub
parent db998d7279
commit e5b0c88297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -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;