It's a classic that when removing elements from an array, you either need to iterate through the array backwards or decrement the index
variable, otherwise you could skip over elements that are right next to each other that both should be deleted.
Also, having 0
for the second argument means "replace 0", or in this case, "remove 0", so changing that to 1
will remove the elements
for(var z = 0; z<numArray.length; z++){
if(numArray[z].includes("-")){
numArray.splice(z, 1)
z--;
}
}
for(var z = numArray.length-1; z >= 0; z--){
if(numArray[z].includes("-")){
numArray.splice(z, 1);
}
}
However, it's easier to just use Array.prototype.filter
. This function returns a new array with only elements that evaluate to true based on the passed in function, so we only want to keep the ones that DO NOT INCLUDE "-"
numArray = numArray.filter((num) => !num.includes("-"));