Mistake in challenge: Restrict Possible Usernames?

TL; DR

The logic in this challenge is, as far as I can tell, self-contradicting: Regular Expressions: Restrict Possible Usernames. Username 007 fulfils all the written requirements for usernames, but is expected to fail the regex check. I suggest the wording of the third username requirement be amended to accurately reflect the unwritten requirement: “there should be at least two alphabet characters at the start of the username”.

Read on…

The description text places the following requirements on usernames:

  1. The only numbers in the username have to be at the end. There can be zero or more of them at the end.
  2. Username letters can be lowercase and uppercase.
  3. Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.

The expected results are:

  • Your regex should match JACK
  • Your regex should not match J
  • Your regex should match Oceans11
  • Your regex should match RegexGuru
  • Your regex should not match 007
  • Your regex should not match 9

The problem

As far as I can tell, 007 fulfils all the requirements for usernames, but is expected to fail.

  • Numbers only at the end? True. They’re also at the beginning, but that’s because there are no letters
  • Zero or more numbers? True.
  • Letters not case-sensitive? True. There are no letters, but nowhere does it say we are required to have letters.
  • At least two characters long? True.
  • If two characters long, are both characters letters? Not applicable; the username is more than two characters long.

The regex suggested in the “hint” page is this:

let userCheck = /^[a-z]{2,}\d*$/i;

The ^[a-z]{2,} part means “there must be at least 2 alphabet characters at the start of the username”. This requirement is not clearly communicated in the challenge. I suggest the wording of the third username requirement be amended to accurately reflect the unwritten requirement: “there should be at least two alphabet characters at the start of the username”.

The test case of 007 fails the above, because 0 is a number and it is not at the end of the username.

The word “only” above tells you no numbers can start at the beginning of the username. However, there can be zero or more number at then end.

The solution in the hint is actually incorrect. A perfectly valid username is “Z97”. The example has been updated on the master branch but has yet to be deployed to the production site.

Hi @RandellDawson, thanks for your response. I don’t understand your logic though.

Could you please give me your definition of “at the end”?

Neither of the zeroes in 007 are the final character, but this shouldn’t be a problem, because the 9 in Z97 isn’t the final character either, and that’s a valid username.

I have taken the rule to mean “any numbers in the username have to be in one contiguous block at the end of the username”. We could also define it with the rule: “a letter can never follow a number”. Both of these statements are true concerning 007.

Regarding this statement:

If that is the intended meaning, then it is not clear in my opinion. It doesn’t explicitly forbid numbers being at the beginning. In the case where the username has no letters, the numbers are at the end (satisfying requirement 1) and at the beginning.

Anyway, the logic in the challenge description and your post implies that usernames must have at least one letter, but this is never actually spelled out anywhere.

We can debate about definitions all day long, but that is why you also get to see the test case to understand what at the end means. It tells you 007 should return false and that is because “The only numbers in the username have to be at the end”. This means none of the numbers can be at the beginning of the string which is the case in 007 which is why it must return false.

You will learn over time that not every single detail will be given to you by your boss or a client. Some things are just implied, which is the case that I mentioned in the revised version of this challenge where Z79 should return true. It meets all the requirements of the challenge.