I have an array as shown below, containing 6 numbers. I'm trying to create a function that deletes a value if it's an even/odd occurance. For example, if I type the function deleteData(myArr,"even")
, it will remove data number 0th, 2nd, and 4th. And the other way around if it's "odd". But when I tried running my code, it didn't work the way i wanted it to go. Can anybody tell me why?
const myArr=[90,34,28,19,26,22];
function deleteData (array,occuranceType){
switch (occuranceType){
case "odd":
for (let i=0;i<array.length;i++){
if (i%2!==0){
array.splice(i,1);
};
};
break;
case "even":
for (let i=0;i<array.length;i++){
if (i%2==0){
array.splice(i,1);
};
};
break;
};
};
deleteData(arr,"odd")
console.log(arr)
This is the result in the console : [ 90, 28, 19, 22 ]