Home > Blockchain >  Javascript - Finding if there is an integer in a string
Javascript - Finding if there is an integer in a string

Time:12-20

I appreciate this is very basic. Please go easy on me, I am at the beginning of my coding journey!

I am trying to establish whether there is an integer anywhere in a string. If there is, it should return true. If not, it should return false.

Here is my code, which is failing as it is returning true even when there are no integers present. I cannot work out why this might be happening.

function findTicketPrices(emailString) {
    let emailArray = emailString.split(" ");
    for (let i = 0; i < emailArray.length; i  ) {
        if (typeof parseInt(emailArray[i]) === "number") {
            return true;
        }
        return false
    } 
}

CodePudding user response:

So couple issues:

  1. Parsing a string into a number, if it fails it returns NaN, and typeof NaN is still a number
  2. You are returning false inside of the loop, so it exits in the first iteration no matter what.
function findTicketPrices(emailString) {
  let emailArray = emailString.split(" ");
  for (let i = 0; i < emailArray.length; i  ) {
    const value = parseInt(emailArray[i]);
    if (typeof value === "number" && !isNaN(value)) {
      return true;
    }
  }
  return false;
}

CodePudding user response:

parseInt(emailArray[i]) always returns a number so typeof parseInt(emailArray[i]) will always be number

I would use Number.isNaN to check if parseInt returned NaN which indicates that it's not a number

Also, you are returning too fast with that return false

function findTicketPrices(emailString) {
    let emailArray = emailString.split("");
    for (let i = 0; i < emailArray.length; i  ) {
        if (!Number.isNaN(parseInt(emailArray[i]))) {
            return true;
        }
    }
    return false
}

const emails = ['[email protected]', '[email protected]']

for (const email of emails) {
  console.log(email, findTicketPrices(email))
}

You can also do it easier with regex

function findTicketPrices(emailString) {
  return Boolean(emailString.match(/\d/g))
}

const emails = ['[email protected]', '[email protected]']

for (const email of emails) {
  console.log(email, findTicketPrices(email))
}

CodePudding user response:

First you are splitting on spaces and not the characters. It should be

const emailArray = emailString.split("");

Your check fails because parseInt('a') returns NaN. And NaN is a number.

console.log(typeof NaN)

If you want to check for a number, it is just a simple reg exp

var str = 'aaaa1bbbbb';
var numRE = /\d/;
console.log(numRE.test(str));

CodePudding user response:

For this problem, you could convert the string to a list of characters, and then loop through that list, and for each character check if it's an integer. To do that you'd use the built in parseInt function and see if passing the current character into this function returns NaN. That can be checked using the built in isNaN function. The following code does this on one line

const findTicketPrices = emailString => emailString.toString().split("").find(c => !isNaN(parseInt(c))) !== undefined

If you don't want to do this on one line, this code will also do the trick

const findTicketPrices = emailString => {
    const emailArray = emailString.toString().split("");
    for(const i in emailArray){
    const character = emailArray[i];
        if(!isNaN(parseInt(character))){
            return true;
        }
    }
    return false;
};

CodePudding user response:

All you need (just index lookup in the string):

function findTicketPrices(emailString) {
  for (let i = 0; i < emailString.length; i  ) {
    if (!isNaN(parseInt(emailString[i]))) {
      return true;
    }
  }
  
  return false;
}

CodePudding user response:

You can do:

const containsInteger = str => /\d/.test(str)
  • Related