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
telephoneCheck(), when called with an invalid number, should returnfalse.
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
