"Regular Expressions: Restrict Possible Usernames”

Hello. I have a small question. I have this solution, and I modify result parameter with dog variabl e:

let username = "JackOfAllTrades";
let userCheck = /^[a-z][a-z]+\d*$/i; // change this line
let result = userCheck.test(dog);
let dog = "c57bT3"

My question is about why this console.log is true? Shouldn’t it return false? This does not check the first character as a letter (true), the second character as a (1 = true, 0 = false, or several letters in a row = true) and then the last as a number = true or several numbers in a row = true or nothing = true?

I think this is the result of hoisting more so than the result of regex. So this might only be happening because dog is being declared after result.

result will still return true even if you simply declare dog and don’t initialize it to anything.

You’re not doing anything with this variable. It won’t have any effect on the rest of the code.

Oh, thanks. Now I see that. I feel so stupid. I didn’t see that. I just tried putting the variable (dog) before of result and yes, it works, yes, it returns false. I am very happy to see the sense. Thanks friends.

Good job working through it. Happy coding!

let userCheck = /^[a-z][a-z]+\d*$/i; 

This was my solution aswell, but I can’t figure out why it doesn’t work? It works fine for every username except “Z97”.

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