Restrict Possible Usernames with if

I have written a solution for the challenge, as i thought it might work, obviously it didn’t. I have looked at the hint section and saw the correct answer eventually.
What I would like to ask is if my code could be made to work and give the same output. And if not, why ?

Your code so far

let username = "JackOfAllTrades";
 // Change this line
if ( username.length > 2) {
    var userCheck = /\d*$/;
} else if ( username.length = 2 ){
    var userCheck = /^[a-z]/;

let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 OPR/58.0.3135.132.

Link to the challenge:

Even if you fixed the above line (you should not be using the assignment operator), your code does not deal with cases where username.length is less than 2. Also, your solution does not satisfy #1 which is “The only numbers in the username have to be at the end.”. The test case of 007 is not handled, because usernames can only end with numbers and not start with numbers.

Also, based on how the tests are designed, your code would only run a single time and then userCheck would be set for all the remaining tests. What this means is since username is first set by the first line **let username = “JackOfAllTrades” all the tests would use your following userCheck:

var userCheck = /\d*$/;

Why? Because JackOfAllTrades has a length great than 2.

Just stick with a single regular expression which is the entire point of the challenge and you will be fine.

I did see that mistake and fixed the line, and yes, you are right, it still did not work. Thank you for the explanations you have given me, i was curious of the “why it won’t work with this”, and i think I got my answer.