Hi, at the moment I am in absolute tears of frustration. I started this exercise about a week ago and could find no easier way to do it than the way Ramon started this morning. I have watched the video up to the point where Ramon starts to tidy his code up a little.
My code is pretty much identical to Ramon’s. But I have a problem I am unable to get past. A pointer would be appreciated. Please don’t tell me I can do this in one line - I get that, but I don’t get this!!!
function telephoneCheck(str) {
const regex1 = /^\d{3}-\d{3}-\d{4}$/;
const regex2 = /^\(\d{3}\)\d{3}-\d{4}$/;
const regex3 = /^\(\d{3}\)\s\d{3}-\d{4}$/;
const regex4 = /^\d{3}\s\d{3}\s\d{4}$/;
const regex5 = /^\d{10}$/;
const regex6 = /^1\s\d{3}\s\d{3}\s\d{4}$/;
const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
const regex8 = /^1\s\(\d{3}\)\s\d{3}-\d{4}/;
const regexes = [regex1, regex2, regex3, regex4, regex5, regex6, regex7, regex8];
for (let regex of regexes){
if (regex.test(str)){
return true;
}else{
return false;
}
}
return true;
}
console.log(telephoneCheck("1 555-555-5555"));
I think it looks OK, I think it should work but…
false
I don’t know why it is false! I cannot see what I am doing wrong
const regex6 = /^1\s\d{3}\s\d{3}\s\d{4}$/;
//const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
const regex8 = /^1\s\(\d{3}\)\s\d{3}-\d{4}/;
// const regexes = [regex1, regex2, regex3, regex4, regex5, regex6, regex7, regex8];
const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
const regexes = [regex7]
You can see what I have greyed out and you can see what I have added in. regex7 is the same - there is no change, they are copies of each other. So why am I now getting…
true
And exactly the same works for regex8.
If I take out regex 7 and 8 and combine them with earlier ones using (1\s)? the earlier ones stop working!!!
I have been working on this for 4 1/2 hours today and I am frustrated. If someone can explain what it is I am doing wrong I would be most grateful