0

I'm trying to remove elements that are the same i.e removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);

const removeFromArray = function(numbers, ...valuesToCheck) {
for(i=0; i<numbers.length; i++){
    for(j=0; j<valuesToCheck.length; j++){
        if(numbers[i] == valuesToCheck[j]) {
            const index = numbers.indexOf(valuesToCheck[j]);
            numbers.splice(index, 1);
            i--;
        }
    }
}
return numbers;
};

The code works when numbers are entered, however when a string is entered i.e removeFromArray([1,2,3,4], "3", 2);, the return on the function doesn't make sense to me. I want to have it so that only numbers will prompt the corresponding number in the array to be removed and other data types such as strings won't have any effect.

Any hints would be appreciated.

Thank you.

6 Answers6

1

Just use === (strictly equal to) or includes

const removeFromArray = (numbers, ...valuesToCheck) => numbers
  .filter(num => !valuesToCheck.includes(num));

console.log(removeFromArray([1, 2, 3, 4], 3, 2));
console.log(removeFromArray([1, 2, 3, 4], "3", 2));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

You could use typeof to detect if valuesToCheck[i] is a number. Something like:

const removeFromArray = function(numbers, ...valuesToCheck) {
for(i=0; i<numbers.length; i++){
    for(j=0; j<valuesToCheck.length; j++){
        if (typeof valuesToCheck[j] === "number") {
           if(numbers[i] == valuesToCheck[j]) {
             const index = numbers.indexOf(valuesToCheck[j]);
             numbers.splice(index, 1);
             i--;
           }
        }
    }
}
return numbers;
};

console.log(removeFromArray([1, 2, 3, 4], 3, 2));
console.log(removeFromArray([1, 2, 3, 4], "3", 2));
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
0

The problem is with the following line

const index = numbers.indexOf(valuesToCheck[j]);

because numbers doesn't contain the value "3" numbers.indexOf("3") will return -1. and numbers.splice(-1,1) means remove the last elemnt from the array

an easy fix will be

const index = numbers.indexOf(numbers[i]);
Daniel Botnik
  • 539
  • 1
  • 6
  • 20
0

You can try something like this.

const array = [1, 2, 3, 4];
function remove(array, ...args) {
  args.forEach(arg => {
    const index = array.indexOf(Number(arg));
    if (index > -1) {
      array.splice(index, 1);
    }
  })
  return array;
}

console.log(remove(array, 1, "2"))
AmD
  • 399
  • 2
  • 12
0

You should use 3 equal signs === for comparison to compare the type and the value

const removeFromArray = function(numbers, ...valuesToCheck) {
for(i=0; i<numbers.length; i++){
    for(j=0; j<valuesToCheck.length; j++){
        if(numbers[i] === valuesToCheck[j]) {
            const index = numbers.indexOf(valuesToCheck[j]);
            numbers.splice(index, 1);
            i--;
        }
    }
}
return numbers;
};
0

Just as an aside you can use filter to make your function a lot simpler.

// Pass in the array and use `rest` parameters to
// gather up everything else
function removeFromArray(arr, ...rest) {

  // And then filter out all those rest values
  // from the array
  return arr.filter(el => !rest.includes(el));
}

console.log(removeFromArray([1, 2, 3, 4], 3, 2));
console.log(removeFromArray([1, 2, 3, 4], 1, 2));

Additional documentation

Andy
  • 61,948
  • 13
  • 68
  • 95