Password Validator - hasUppercase and hasLowercase functions


#1

I am using the below code and through process of elimination have realised there’s something wrong with the hasUppercase function and hasLowercase function. Please help! The output i get is that every password is invalid. However if I remove the ‘return false;’ statements from hasUppercase() and hasLowercase() then any passwords which fit the criteria EXCEPT for having both upper and lower case characters are marked as valid.

function hasUppercase(input)
{
  for(i = 0; i < input.length; i ++)
    {
      if(input[i] === input[i].toUpperCase())
        {
          return true;
        }
      return false;
    }
}

function hasLowercase(input)
{
  for(i = 0; i < input.length; i ++)
    {
      if(input[i] === input[i].toLowerCase())
        {
          return true;
        }
      return false;
    }
}

function isPasswordValid(input)
{
  if(hasUppercase(input) && hasLowercase(input) && has8Characters(input) && hasSpecialCharacter(input))
    {
      console.log('Password is valid');
    }
  else
    {
      console.log('Password is invalid');
    }
}

#2

It’s because you have return false statements inside the for loop, so it only actually checks the first letter before returning true or false


#3

Cheers for the reply, my functions now look like this, however they still class ‘tt!tttttt’ and ‘TT!TTTTTTTT’ as valid passwords so something must be going wrong in the validation part right?:

function hasUppercase(input)
{
  for(i = 0; i < input.length; i ++)
    {
      if(input[i] === input[i].toUpperCase())
        {
          return true;
        }
    }
  return false;
}

function hasLowercase(input)
{
  for(i = 0; i < input.length; i ++)
    {
      if(input[i] === input[i].toLowerCase())
        {
          return true;
        }
    }
  return false;
}```

#4

ah, it’s because any symbols, such as !, to lower case or to upper case is the same.
’!’ == ‘!’.toLowerCase() == ‘!’.toUpperCase()

You’d have to either check each character in the loop to see if its a letter first, or remove all non-alphabet characters at the beginning of the hasLower and hasUpper functions


#5

Oh yes good point! I’ve amended it to the below and unfortunately those 2 passwords used above still register as valid. Any more ideas?

function hasUppercase(input)
{
  var specialCharacters = ['!', '?', '-', '_', '@'];
  for(i = 0; i < input.length; i ++)
    {
      for(j = 0; j < specialCharacters.length; j++)
        {
          if(input[i] !== specialCharacters[j])
          {
            if(input[i] === input[i].toUpperCase())
        		{
          		return true;
        		}
          }
        }
    }
  return false;
}

function hasLowercase(input)
{
  var specialCharacters = ['!', '?', '-', '_', '@'];
  for(i = 0; i < input.length; i ++)
    {
      for(j = 0; j < specialCharacters.length; j++)
        {
          if(input[i] !== specialCharacters[j])
          {
            if(input[i] === input[i].toLowerCase())
        		{
          		return true;
        		}
          }
        }
    }
  return false;
}


#6

on the second iteration of for(j =0 …)
it evaluates to ‘!’ !== ‘?’ which is true so then it still does ‘!’.toUpper or toLower.

looking at your code it appears you should have a function which is hasSpecialCharacter, you could pass each letter to that in the loop

so you could have:

for(i = 0; i < input.length; i ++)
    {
      if(!hasSpecialCharacter(input[i])){
         if(input[i] === input[i].toUpperCase())
           {
             return true;
           }
       }
    }
  return false;

#8

Cracked it! Thanks for your help, this is how they ended up:

function hasUppercase(input)
{
  for(i = 0; i < input.length; i ++)
    {
      if(!hasSpecialCharacter(input[i])){
         if(input[i] === input[i].toUpperCase())
           {
             return true;
           }
       }
      else
        {
          return false;
        }
    }
  return false;
}

function hasLowercase(input)
{
 for(i = 0; i < input.length; i ++)
    {
      if(!hasSpecialCharacter(input[i])){
         if(input[i] === input[i].toLowerCase())
           {
             return true;
           }
       }
      else
        {
          return false;
        }
    }
  return false;
}```