Restrict Possible Usernames Should it be wrong?

Tell us what’s happening:

That’s my answer and the program gives it to me as correct, but when I see the hint’s answer it’s very different from mine.

Your code so far


let username = "JackOfAllTrades";
let userCheck = /.\D./i; // Change this line
let result = userCheck.test(username);
console.log(result);

Your browser information:

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

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

This also correct, I would like to understand why.

let username = "JackOfAllTrades2442";
let userCheck = /[a-z]\d*/ig; // Change this line
let result = userCheck.test(username);
console.log(result);

There are 3 conditions and both two regex it’s not exact:

  1. The only numbers in the username have to be at the end. There can be zero or more of them at the end.

\d*$

Important: $ means at the end of string

  1. Username letters can be lowercase and uppercase.

[a-zA-Z]

  1. Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.

[a-zA-Z]{2,}

This is my solution /^[a-zA-Z]{2,}\d*$/

1 Like