dimanche 19 janvier 2020

Sort number using regex

I had a string to sort number from the smallest to the biggest:

  1. sortNumber(“4678777ad5”) output: 45677778
  2. sortNumber(“7rr75”) output: 577
  3. sortNumber(“rr”) output: 'No number'

I've tried this code:

const sortNumber = input => {
     const result = input
          .split("")
          .filter(item => /[0-9]/g.test(item))
          .map(item => Number(item))
          .sort((a, b) => a - b);
     console.log(result);
};

I confused about the logical statement to check the input. Please give each process console.log so I know what the data looks like and what it wants to be. Thanks

Aucun commentaire:

Enregistrer un commentaire