JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

This is about one of the evaluation tests, so I don’t want much help, but I am not sure if I can call a function from an if statement nested in a for loop and do a return that breaks me out of the global function that I am in or if the return just breaks me from the for and/or if. Basically, I am asking if return treats either an if or a for as a function which would mean I am only breaking from a subfunction and not the one I really want to break from. I don’t think I should get more help than this clarification on the return functionality in this situation.

  **Your code so far**
function telephoneCheck(str) {
let digits=str.replace(/\D/g, '');
let length=digits.length;
let newStr="";
let patterns=[/[^\(\)-\s\d]/g, /\(^\d{3}^\)/g];
let passfail=true
if (digits[0]=="1" && length==11) {
  newStr=str;
} else if (digits[0]!="1" && length==10) {
    newStr="1 "+str;
  } else {
    return false;
  }

console.log(newStr, patterns[0].test(newStr), patterns[1].test(newStr));

for (let i=0; i<patterns.length; i++) {
  if (patternTest(newStr, patterns[i])){
    return false;
  }
}
//It seems that I have eliminated all types of falses that matter.
return true;

function patternTest(s,p) {
  return p.test(s)
}
} 

telephoneCheck("(555)5(55?)-5555");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Link to the challenge:

Hi

Would you be so kind to indent your code a bit? It’s hard to read right now.

return will stop the whole thing, function I mean. No matter how deep it is nested inside the loops or if’s

So when if condition is fired, and you have return statement inside it - that’s it, function will return stuff, all lower part of the function’s code will not be executed in this case.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.