filtering numbers from array with the digit 5

Viewed 186

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.

5 Answers

There are a couple of issues I see:

  1. You're using numbersStr.splice(counter, 1); to remove a character from the string that you're looping through. That means that everything after the point where you've removed that character has been moved forward one slot. But since you're looping forward, and you don't do anything with your index variable when you remove the character, you'll skip your check of the next character. You want to either loop backward, or subtract one from i when doing the splice.

  2. You're using filter incorrectly (as just a looping construct). filter is for creating a new array with only a subset of the entries from the original array. In your case, you're creating that array, but it will always be empty because your filter callback never returns a truthy value. (Later you fill in that array with entries from numbersStr.)

Typically you'd either remove the 5s from the strings, or create an array with only the numbers that don't have 5s in them, but not both.

To remove the entries with 5s in them, you can use just filter, and you can use includes("5") (or indexOf("5") !== -1) to see whether the string contains the digit 5:

const noFives = numbers.filter(number => !String(number).includes("5"));

Live Example:

const numbers = [3, 5, 56, 58, 85, 95, 105, 905, 556];
const noFives = numbers.filter(number => !String(number).includes("5"));
console.log(noFives);

You can do this in a one-liner using .filter to filter elements of numbers, .toString to convert each element to a string, and .includes to check if this string contains 5:

const numbers = [3, 5, 56, 58, 85, 95, 105, 905, 556];

const noFives = numbers.filter(x => !x.toString().includes("5"));

console.log(noFives);

I simply need to filter from the array all the numbers that has 5 in it.

I assume you want to filter the number which have 5 in it. In the filter callback just use toString to convert it into string and then use includes

const numbers = [3, 5, 56, 58, 85, 95, 105, 905, 556];
const noFives = numbers.filter(x => {
  return x.toString().includes(5)
});

console.log(noFives); /* (5) [3, 56, 85, 105, 556]  */

Alternatively you can also use indexOf function.

const numbers = [3, 5, 56, 58, 85, 95, 105, 905, 556];
const noFives = numbers.filter(n => (n+'').indexOf('5') < 0);
console.log(noFives);

maybe this help you

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;
let arrfive = [];
let noFive = [];
let flag = false;
const noFives = numbersStr.filter(x => {
  flag = false;
  for (let i = 0; i < x.length; i++) {
    if (x[i] == "5") {
      arrfive.push(x);
      flag = true;
    }
  }
  if(!flag){
    noFive.push(x);
  }
});

console.log(arrfive);
console.log(noFive);

Related