I have simple array with numbers only. I simply need to filter from the array all the numbers that has 5 in it. examples of excluded numbers: 5, 56, 557,965 etc.. I got Strange behavior and only some of the numbers are deleted.
here's what I tried:
const numbers = [3, 5, 56, 58, 85, 95, 105, 905, 556];
const numbersStr = [];
// Turn "numbers" const to strings
for (const number of numbers) {
numbersStr.push(number.toString());
};
// filtering Fives
let counter = 0;
const noFives = numbersStr.filter(x => {
for (let i = 0; i < x.length; i++) {
if (x[i] == "5") {
numbersStr.splice(counter, 1);
}
}
counter++;
});
// end: filtering fives
// turn no fives to numbers
for (const strNumber of numbersStr) {
let num = Number(strNumber);
noFives.push(num);
}
console.log(noFives); /* (5) [3, 56, 85, 105, 556] */
The desired result should be [3] only.