Alternate Solution to RegExp Problem?

I think this is also another valid solution to the challenge, and doesn’t need the starting ^.

let username = "JackOfAllTrades";
let userCheck = /([a-z])([a-z]+|[0-9][0-9]+)$[0-9]*/i; // Change this line
let result = userCheck.test(username);

Challenge:

1 Like

If you don’t specify starting ^ you don’t check the start of the string:

userCheck.test('0a00');
userCheck.test('!@#$%arjunnnij');
// etc...

So it’s not a valid solution, it’s a valid hack of insufficient tests

2 Likes

Got it, thanks for the example!

Please include the use of parentheses in the curriculum.
This seems to work:
let userCheck = /^[A-z][\d][\d]+$|^[A-z][A-z]+\d*$/;

1 Like

this works too, not sure if its ok
let userCheck = /^[\D]([\d]{2,}|[\D]+\d*)$/i

This one worked for me: /(^\D{2,}$)|(^\D+\d{2,}$)/

The breakdown for me was:

  1. If it must start with a letter, then I should use ^\D.
  2. Since I know that the minimum length is 2 and that requires 2 letters, then the first thing I need to check for is ^\D{2,} at a minimum.
  3. In all other cases, I know that the username must start with a letter and, if it has a number, it needs to end with the number. In this case, I figured that ^\D+\d{2,}$ would be the best option.
  4. Since these are two different scenarios for the username, and I didn’t want to have one that was super long, I used the | to say that it could be either.

Not sure if this will work for all scenarios, but it worked for this one.

2 Likes

Wrongly sorted. This problem is out of sequence, it requires the use of “number of matches” among other things that are only seen later.

Following code will help :-

let username = "JackOfAllTrades";
let userCheck = /^[a-z](\d\d+|\D+\d*)$/i;
let result = userCheck.test(username);

1 Like
let username = "JackOfAllTrades";
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username);

Code Explanation

  1. ^ - start of input
  2. [a-z] - first character is a letter
  3. [0-9]{2,0} - ends with two or more numbers
  4. | - or
  5. [a-z]+ - has one or more letters next
  6. \d* - and ends with zero or more numbers
  7. $ - end of input
  8. i - ignore case of input

Hi, need an explanation on why the suggested solution matches the string “JACK” although we specified (no. 6 - and ends with zero or more numbers) it to end with a number? Also, where does the check for the numbers, in the strings, “BadUs3rnam3” and “c57bT3”, that are not in the end, happens? Thanks in advance.

1 Like

“JACK” ends in zero numbers, so it is a match.

Here:

([0-9]{2,}|[a-z]+\d*)$

Thanks you :pray:t2:

Hi there,
Please allow me to share with you the solution I made for this exercise, based on the given premise or rules. To test for the validity of any username data entry, my solution is, as follows:

/*You may try feeding the sample usernames from the exercise to test the code;
it shall suffice to demonstrate the code works for any username entry, based on the given premise.

Thanks.
*/

your function is missing one thing: it just returns undefined in each case, so it wouldn’t pass the tests

Here is my solution:

/^([a-z]+\d{2,}|[a-z]{2,})\d*$/i

It passed all the tests, but how would I evaluate how robust it is? Is there something I can improve?

Thanks!