Restrict Possible Usernames -- help

Tell us what’s happening:

I don’t get how considering the regex lessons so far do I ensure the number will only be at the end and to limit the characters to be at least 2 characters long and they only use alphabets if 2 characters long

Your code so far


let username = "JackOfAllTrades";
let userCheck = /\w+\d/i; // Change this line
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/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames

@JM-Mendez @kevinSmith

Thanks, two of the links you pasted here are future lessons why do this at this stage then?

Because coding. lol. But seriously, think of it as another lesson. Many times in the dev world you’ll have to research things beyond what the specs ask, just so that you can accomplish a task.

So if you look at it as a lesson in researching, then it’s not too soon :slight_smile:

I absolutely understand, this was something FCC encouraged a lot in the previous version but I think its absolutely stupid to not mention that you might have to search on google or read the docs or something. It is like thinking outside the box for the sake of thinking outside the box, it is pretentious.

Earlier they made you learn things and then didn’t have a lesson explaining that same thing and that is true now.

1 Like

You can also do something like this :

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

I don’t think it’s using any of the following lessons.

3 Likes

Thanks that is good, but what in the code ensures the password is at least 2 chars long and what part ensures if 2 then only alphabets? @camperextraordinaire @JM-Mendez @Capucine

The first [a-z] ensures that the first element is a letter,
the second [a-z] does the same for the second element
and the + lets the password be longer if needed.
Because there is two [a-z], the password will be at least 2 characters long.

The code would be better with an ^ at the beggining, because with my previous code something like 7password would pass, I think.

Hope this helps :slight_smile:

2 Likes

Definitely helped, thanks!

For the second [a-z] I accidentally did [A-Z] and that also worked. Any insights on that?

Yes, /i makes it so the regex will look for both uppercase and lowercase letters, no matter which one you wrote in your expression!

1 Like

Yes! of course. Dumb thing to ask

Don’t worry, there is no dumb question :slight_smile:

1 Like