0

I have been trying to write a function that takes an array of usernames and return true if they are all valid and false if any are not valid. A valid username:

  • is at least 5 characters long
  • may only contain lowercase letters, numbers and underscores
  • is no longer than 20 characters

eg - ["validuser_72_", "valid", "invaliduser_72*", "1234567", "val__67"] returns false.

  • ["validuser_72_", "valid", "1234567", "val__67"] returns true.

The following code will pass the tests in my test suite:

function checkUsernames(users) {

  if (!users || users.length === 0) return false;

  const regex = /[A-Z\W]/g;

      
  return users.every(
    (user) => !regex.test(user) && user.length < 21 && user.length > 4
  );
}

However when I add a console.log, as per the code below, to test my inputs it seems to be affecting the return value in unpredictable ways.

function checkUsernames(users) {

  if (!users || users.length === 0) return false;

  const regex = /[A-Z\W]/g;

  console.log(users.every(
    (user) => !regex.test(user) && user.length < 21 && user.length > 4
  ));

  return users.every(
    (user) => !regex.test(user) && user.length < 21 && user.length > 4
  );
}

Can anyone explain why console logging in this way should have any affect on the return value?

Thanks!

Tubes
  • 1
  • 1

0 Answers0