0

I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -

If there are two separate numbers in the string - return the first of them.

So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:

function count(apples) { 
  const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
  console.log(number);
}
buholica
  • 1
  • 1
  • 1
    `Number(apples.match(/\d+/)[0])` – adiga May 02 '21 at 12:19
  • In case you, for ex., have `const text = 'Some text with 765 integer number'`, then `Number(text.match(/\d+/)[0]);` returns `765`, and `Number(text.match(/\d/)[0])` - `7`. I believe the second one is your case. Take a look https://jsfiddle.net/vyspiansky/xtnj63ug/ – Ihor Vyspiansky May 02 '21 at 12:25
  • Thanks) But it doesn't work for me. I have other strings for input to check my code, so the code doesn't work correctly with those strings. – buholica May 02 '21 at 12:39

0 Answers0