1

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
    } 
}
2
  • 1
    so just use a regular expression Commented Dec 19, 2022 at 18:25
  • parseInt returns NaN when given an invalid argument. Despite its name, NaN is, in fact, of type number. Try changing your if statement to instead check if the result is NaN, rather than seeing if the type is number. For this, the Number.isNaN() function will help you. Commented Dec 19, 2022 at 18:26

6 Answers 6

4

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Looks good except I think you'd need .split(''), i.e., no single space but rather blank string/space instead.
I originally considered that, but what if OP did it for a reason? Like accounting for a value where there is a number and a letter in the argument. hello 7d 77 would result in 7d being a false positive. I understand that it's supposed to be an email? but still worth considering.
Might just be a string of emails separated by spaces.
But will an email have any blanks to split on? Also, you can just index lookup a character in a string. No need to turn into an array first.
Just index the string and don't worry about splitting into an array.
|
1

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))
}

Comments

0

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));

Comments

0

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;
};

Comments

0

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;
}

Comments

0

You can do:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.