Password validator won't run


#1

My passwordValidator doesnt work well!

passwordValidator exercise
https://www.codecademy.com/en/courses/learn-javascript/projects/password-validator?link_content_target=interstitial_project

I don't get error messages. But no matter what password I test, it ALWAYS says that it misses a capital letter, a small letter, a special character and is not 8 characters long. What is missing?
HELP please!

function hasUpperCase(input) {
  for (var i=0; i < input.lenght; i++) {
   if (input[i] === input[i].toUpperCase()) {
     return true;
   }
  }
}

function hasLowerCase (input) {
  for (var i = 0; i < input.lenght; i++) {
   if (input[i] === input[i].toLowerCase()) {
     return true;
   }
  }
}
function isLongEnough (input) {
  if (input.lenght >= 8)
    return true;
}
function hasSpecialCharacter(input) {
  var specialCharacter = ['!', '?', '$', '#', '@', '*', '&'];
  for (var i=0; i < input.lenght; i++) {
   for (var j=0; j < specialCharacter.length; j++) {
     if (input[i] === specialCharacter[j]) {
       return true;
     }
 }
}
}

function isPasswordValid(input) {
  if (hasUpperCase(input) && hasLowerCase(input) && isLongEnough(input) && hasSpecialCharacter(input)) {
    console.log('The password is valid.');
  }
  if (!hasUpperCase(input)) {
      console.log('The password needs a capital letter.');
      }
  if (!hasLowerCase(input)) {
    console.log('The password needs a small letter.');
  }
  if (!isLongEnough(input)) {
    console.log('The password needs to be at least 8 characters long.');
  }
  if (!hasSpecialCharacter(input)) {
    console.log('The password must contain at least one special character.');
  }
}
isPasswordValid('PSaaaaa&');

// output: The password needs a capital letter.
The password needs a small letter.
The password needs to be at least 8 characters long.
The password must contain at least one special character.


#2

input.lenght;

You misspelled length quit a few times


#3

Thanks a lot! :slight_smile: It works now :smiley:

How did you find the error? Did you use some tool/program for that or you did it yourself?

I´m just a beginner, trying to get better.


#4

Not some special tool, just in the codecademy lesson

i simply added a console.log() to see if return true would be reached:

function hasUpperCase(input) {
  for (var i=0; i < input.lenght; i++) {
   if (input[i] === input[i].toUpperCase()) {
     console.log(true);
     return true;
   }
  }
}

not really anything that happened. i put a uppercase letter in your password, then i did:

function hasUpperCase(input) {
  for (var i=0; i < input.lenght; i++) {
   if (input[i] === input[i].toUpperCase()) {
     return true;
   }
  }
  console.log("i get here, while i shouldn't");
}

a function ends the moment a return keyword is reached, so console.log shouldn't run, but it did.

so then did probably something like:

function hasUpperCase(input) {
  for (var i=0; i < input.lenght; i++) {
   console.log(i);
   if (input[i] === input[i].toUpperCase()) {
     return true;
   }
  }
}

this should log i multiply times to the console (depending on the length of your password)

At this point, you determined something is wrong with the loop, so you take a close look at each letter


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.