0

I am trying to solve a challenge from jshero.net. The challenge is:

Write a function parseFirstInt that takes a string and returns the first integer present in the string. If the string does not contain an integer, you should get NaN. parseFirstInt('No. 10') should return 10 and parseFirstInt('Babylon') should return NaN. The solution I came up with is:

function parseFirstInt(num){
let input=parseInt();
if(Number.isNaN(num)){
return NaN} else {
return num[0]}

}

But it doesn't work. It returns the following errors:

parseFirstInt('No. 10') does not return 10, but 'N'.

Test-Error! Correct the error and re-run the tests!

Do you guys have any ideea how to solve it?

  • 1
    Does this answer your question? [Get the first integers in a string with JavaScript](https://stackoverflow.com/questions/609574/get-the-first-integers-in-a-string-with-javascript) – Tu.Ma. Jun 23 '20 at 11:59
  • `num.match(/\d+/)?.[0] ?? NaN` – Ivar Jun 23 '20 at 12:05
  • If you just want it to work, and don't care if the solution is ugly: use digitFound flag set to false, result set to NaN, done set to false, then, iterate over the chars of the string, as long as not done and not end of string, if digit and not digitFound: result=digit, if digit and digitFound: result+=digit, if not digit and digitFound: done=true. When loop exits, int=Number(result). – iAmOren Jun 23 '20 at 12:11
  • I tried by using `num.match(/\d+/)?.[0] ?? NaN` but I get the following error: **parseFirstInt('No. 10') does not return 10, but undefined. Test-Error! Correct the error and re-run the tests!** – DefNotBruceWayne Jun 23 '20 at 12:16
  • @DefNotBruceWayne Did you return it? `function parseFirstInt(num){return num.match(/\d+/)?.[0] ?? NaN;}` – Ivar Jun 23 '20 at 12:17
  • First I did it without the `return` and now I used it and it gave me the following error: **parseFirstInt('No. 10') does not return 10, but '10'. Test-Error! Correct the error and re-run the tests!** – DefNotBruceWayne Jun 23 '20 at 12:19
  • Ah they want you to return a number type. Use `function parseFirstInt(num) { return parseInt(num.match(/\d+/)?.[0], 10); ` – Ivar Jun 23 '20 at 12:21
  • Well, your code worked better than mine anyway.I think the program is at the last test because now it returns : **parseFirstInt('sum: -120') does not return -120, but 120. Test-Error! Correct the error and re-run the tests!** – DefNotBruceWayne Jun 23 '20 at 12:26
  • 1
    Negative numbers as well. You can add an optional `-` to the regex: `return parseInt(num.match(/-?\d+/)?.[0], 10);` – Ivar Jun 23 '20 at 12:29

4 Answers4

0

I would definitely use regex for this.

Use what's written here in order to replace all Strings with nothing

Get the first integers in a string with JavaScript

Then after that I would check if the length is 0. If it is return NaN if not then parseInt

Amit
  • 157
  • 8
0

this should work :

    function parseFirstInt(text){
    
       for(let i=0;i<text.length;i++){
          if(text.charCodeAt(i)<58 && text.charCodeAt(i)>47){
              return text[i];
           } 
       }
       return NaN;
    }
Abdel-hilmi
  • 73
  • 1
  • 8
  • I tried it now and I got the following error: **parseFirstInt('No. 10') does not return 10, but '1'. Test-Error! Correct the error and re-run the tests!** – DefNotBruceWayne Jun 23 '20 at 12:29
  • i thought u wanted the first number of the `Int` sorry , If u want the whole Int you should use `matches=text.match(/(\d+)/);` and return ` matches[0]`. – Abdel-hilmi Jun 23 '20 at 12:46
0

Using a /[-+]?[0-9]+/g in match() function, you can find and parse the first integer present in the string.

function parseFirstInt(num){
  const ints = num.match(/[-+]?[0-9]+/g)
  return ints ? Number(ints[0]): NaN
}

console.log(parseFirstInt('No. 10'));
console.log(parseFirstInt('NaN'));
console.log(parseFirstInt('No. -10'));
Shuvojit Saha
  • 382
  • 2
  • 11
0

This is my solution to this problem:

const parseFirstInt = str => {
    for (let i = 0; i < str.length; i++) {
        let temp = str.substr(i)
        let result = parseInt(temp);
        if (!(Number.isNaN(result))) {
            return result;
        }
    }
    return NaN
}