Issue with Telephone Number Validator?

Hello this is my first time posting in the forum and sharing my code in public i know its messy and stuff, but it passed the 30 test cases except 1 thats

  1. telephoneCheck(), when called with an invalid number, should return false.

can anyone hint why the last one failed Ive tried all I can to pass the last test case, but no luck so far guys. If anyone have any idea how to fix this or if you see something wrong with the code dont hesitate to make a pointer thank youuu!

function telephoneCheck(phone) {

if (!phone || phone < 0 || phone == 0 || typeof phone !== "string") return false;




let valid = \["0","1","2","3","4","5","6","7","8","9"\];



let count = 0;

if(phone.includes("(")){

let indexOpen = phone.indexOf("(");



let indexClose = phone.indexOf(")");



let distance = indexClose - indexOpen; 

if (distance !== 4){

return false;

}

}






if(phone.includes(")")){



if(!phone.includes("(")){

return false;

}

}



let open = "(";

let close = ")";

let dash = "-";

let parenCount = 0;

let dashCount = 0;

for (let i = 0; i<phone.length;i++){

if (phone\[i\] == open){

        parenCount++;

        console.log("open",phone\[i\])

}

if (phone\[i\] == close){

        parenCount++;

        console.log("close",phone\[i\])

}



if (phone\[i\] == dash){

        dashCount++;

        console.log("dash",phone\[i\])

}



}



if(parenCount > 2){

return false;

}



if(parenCount == 1){

return false;

}



if(dashCount > 2){

return false;

}





for(let i =0; i<phone.length; i++){

let isValid = false;

for (let j = 0; j<valid.length;j++){

if (phone\[i\] == valid\[j\]){

         isValid = true;

}   

}



if (isValid){count++}    

}



    console.log(count)



if (phone.startsWith("1") && count == 11){

return true;    

}



if (count == 10){

return true;

}



return false;

}

type or paste code here

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Welcome to the forum @reminiscedfuture !

Please post a link to this challenge.

Thank you.

this one

the link to the challenge is

What if the phone number is “555-555-1234x”?

yes thank you for pointing that out. I actually take a bit of time to look at the entire code again. I thought i was just counting numbers and checking certain conditions but never really to look if theres more unwanted inputs thank you.

if someone is already confused and found this thread.

I used

removed by moderator

to solve it. Thank you dhess for your help.

Good job figuring it out, but I’ve removed your solution so it won’t be available to others who have not yet done the work to get there. Again, congrats!

1 Like