1

I'm working on a JS exercise and it semi works but it's not returning a statement when the last conditional is satisfied, only an empty set of parenthesis. I'm not too sure why but any help is appreciated!

function isItANum(str) {
  var phonenum = str.replace(/[^0-9]+/g,"");

  if(str.length===11 && str.indexOf(0)===0) {
    str = str;

  } else if (str.match(/[a-z]/i)) {
    str = phonenum;

  } else {
    str = "Not a phone number";
  }

  return str;
}

isItANum("hey");

2 Answers 2

2

Simply step through the calculation in order.

First, there are no digits in hey, so the variable phonenum gets replaced with an empty string when it reaches str.replace(/[^0-9]+/g, "");.

Second, you check if the length is 11, and if the first character isn't a 0. That fails, so it steps into the else if.

Third, hey matches the regex /[a-z]/i, so str is set to phonenum.

Fourth, str (now phonenum) gets returned, and is an empty string.

To actually return "Not a phone number", simply remove the else if entirely; the non-digits will already be stripped by the first regex. That way, when the if fails, it will go straight to the else. You can combine the length and starting 0 validation in the same conditional.

Note that you're probably actually looking for the regex /^[a-z]/i to ensure that it only contains lowercase characters (note the starting carat). And for what it's worth, the first if calculation is unnecessary, as str is already exactly equal to str.

This can be re-written as the following:

function isItANum(str) {
  var phonenum = str.replace(/[^0-9]+/g,"");
  if (str.length === 11 && str.indexOf(0) === 0 && str.match(/[^a-z]/i)) {
    str = phonenum;
  } else {
    str = "Not a phone number";
  }
  return(str);
}

console.log(isItANum("hey"));
console.log(isItANum("111111111"));
console.log(isItANum("01111111111"));

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Glad to hear this solution appears to have helped you. Once you've confirmed this solution solves your problem, please don't forget to mark the solution as correct by clicking on the grey check below the vote buttons -- this removes it from the 'Unanswered Questions' queue, and awards reputation to both the question asker and question answerer. Of course, in saying that, you are under no obligation to mark my answer (or any other answer) as correct, though it does help to keep things on StackOverflow flowing smoothly :)
0

At the second if, the variable str is already equal to "" . so you need to add another if the str doesnt have any numbers and the value of str is equal to "", its not a phone number.

function isItANum(str) {
var phonenum = str.replace(/[^0-9]+/g,"");

if(str.length===11 && str.indexOf(0)===0) {
 str = str;
} 
else if (str.match(/[a-z]/i)) {
  str = phonenum;
} 

if (str === '') {
  str = "Not a phone number";
}
console.log(str);
return str;
}

isItANum("hey");

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.