Hello guys i completed these telephone number validator.
Please do check and review it.
Your reviews will help me to improve my code and get better.
Thank you
function telephoneCheck(str) {
// Good luck!
var sid = str.match(/-*[^-()\s]/g);
sid[0] = parseInt(sid[0]);
for(var i = 1;i<sid.length;i++){
sid[i] = Math.abs(sid[i]) || sid[i];
}
var bal = false;
console.log(sid.length);
if(sid[0]===1 && sid.length===11){
if(str.indexOf('(')>=0 || str.indexOf(')')>=0){
bal = braCheck(str);
}
else
bal = true;
}
if(sid.length==10){
if(str.indexOf('(')>=0|| str.indexOf(')')>=0){
bal = braCheck(str);
}
else
bal = true;
}
console.log(bal);
return bal;
}
function braCheck(str){
if((str.indexOf(')')-str.indexOf('(')===4 ) && (str.indexOf('(') >=0 && str.indexOf(')') >=0))
return true;
else
return false;
}
telephoneCheck("1 456 789 4444");
I don’t know why I tend to pass every data to an Array… I feel that it is easier, your code is really good, but perhaps using arrays will be easier for you to get the answer…
-
maybe those “fors” can be changed for a map method…
-
In this part of the code:
if(sid[0]===1 && sid.length===11){
if(str.indexOf('(')>=0 || str.indexOf(')')>=0){
bal = braCheck(str);
}
else
bal = true;
}
if(sid.length==10){
if(str.indexOf('(')>=0|| str.indexOf(')')>=0){
bal = braCheck(str);
}
else
bal = true;
}
you are expressing the same in both ifs, why not just merge both if and add the expression ||
if(sid[0]===1 && sid.length===11||sid.length==10){
if(str.indexOf('(')>=0|| str.indexOf(')')>=0){
bal = braCheck(str);
}
else
bal = true;
-
I repeat there is a great code, day by day we are going to improve ourselves.
-
This is my code I hope another buddy can tell me what can I do to become a better programmer.
function telephoneCheck(str) {
// Good luck!
if (str.length <10||str.length>16||str[0]==2||str[1]==2||(str.indexOf("(",0)>=0!=str.indexOf(")",0)>0)){
return false
}
else if ((str.split("").map(function (x) {if (Number(x)>0){
return Number(x)}else {return x}}).every(x=> typeof(x)=="number"||x===" "||x==="("||x===")"||x==="-"))==true){return true} else {return false}
}
telephoneCheck("(555)555-5555");