Ad
Code
Diff
  • function testPassword(password){
    
      let tests = [hasUppercase,hasSpecial,isLong];
      // every test must pass
      return tests.every(func=>func(password));
    }
    
    // checks if password has a special character in it
    function hasSpecial(password) {
      
      return password.split('').some((char) => {
        
        let charCode = char.charCodeAt();
        
        if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64)){
          return true;
        } else {
          return false;
        }
      });
    }
    
    // checks if a password has an uppercase character in it
    function hasUppercase(password) {
      return password.split('').some((char) => {
      
        let charCode = char.charCodeAt();
        
        if(charCode > 64 && charCode < 91){
          return true;
        } else {
          return false;
        }
      });
    }
    
    // checks if a password meets the length requirement
    function isLong(password) {
    
      if(password.length > 7){
        return true;
      }
    }
    
    • function testPassword(password){
    • let hasUppercase = false;
    • let hasSpecial = false;
    • let isLong = false;
    • let tests = [hasUppercase,hasSpecial,isLong];
    • // every test must pass
    • return tests.every(func=>func(password));
    • }
    • // checks if password has a special character in it
    • function hasSpecial(password) {
    • password.split('').forEach((char) => {
    • return password.split('').some((char) => {
    • let charCode = char.charCodeAt();
    • if((charCode > 32 && charCode < 48) || (charCode > 57 && charCode < 64)){
    • hasSpecial = true;
    • }
    • else if(charCode > 64 && charCode < 91){
    • hasUppercase = true;
    • return true;
    • } else {
    • return false;
    • }
    • });
    • }
    • // checks if a password has an uppercase character in it
    • function hasUppercase(password) {
    • return password.split('').some((char) => {
    • let charCode = char.charCodeAt();
    • if(charCode > 64 && charCode < 91){
    • return true;
    • } else {
    • return false;
    • }
    • });
    • }
    • // checks if a password meets the length requirement
    • function isLong(password) {
    • if(password.length > 7){
    • isLong = true;
    • return true;
    • }
    • return hasUppercase && isLong && hasSpecial;
    • }
    • }

combining the loops

Code
Diff
  • function testPassword(password){
      var cap = false;
      var spec = false;
      var number = false;
      var temp;
      
      for(i=0;i<password.length;i++){
      temp = password[i].charCodeAt();
        if(temp > 32 && temp < 48){
          spec = true;
        } else if(temp > 57 && temp < 64){
          spec = true;
        } else if(temp > 64 && temp < 91){
          cap = true;
        }
      }
    
    
    //see if the password is over 8 digits long
    if(password.length > 7){
      number = true;
    }
      
      //provide final answer
      if(cap && number && spec){
        return true;
      } else {
        return false;
      }
    
    }
    • function testPassword(password){
    • var cap = false;
    • var spec = false;
    • var number = false;
    • var temp;
    • for(i=0;i<password.length;i++){
    • temp = password[i].charCodeAt();
    • if(temp > 32 && temp < 48){
    • spec = true;
    • } else if(temp > 57 && temp < 64){
    • spec = true;
    • } else if(temp > 64 && temp < 91){
    • cap = true;
    • }
    • }
    • //check each digit and see if any are capital letters
    • for(i=0;i<password.length;i++){
    • if(password.charCodeAt(i) > 64 && password.charCodeAt(i) < 91){
    • cap = true;
    • }
    • }
    • //see if the password is over 8 digits long
    • if(password.length > 7){
    • number = true;
    • }
    • //provide final answer
    • if(cap && number && spec){
    • return true;
    • } else {
    • return false;
    • }
    • }