企业面试真题--018

题目来源:牛客网

现公司要开发一个业务管理系统,要求注册环节的密码需要提示用户其安全等级,密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。

一、密码长度:

5 分: 小于等于4 个字符 10 分: 5 到7 字符 25 分: 大于等于8 个字符

二、字母:

0 分: 没有字母 10 分: 全都是小(大)写字母 20 分: 大小写混合字母

三、数字:

0 分: 没有数字 10 分: 1 个数字 20 分: 大于1 个数字

四、符号:

0 分: 没有符号 10 分: 1 个符号 25 分: 大于1 个符号

五、奖励:

2 分: 字母和数字 3 分: 字母、数字和符号 5 分: 大小写字母、数字和符号

最后的评分标准:

大于等于 90: 非常安全 大于等于80: 安全 大于等于70: 非常强 大于等于 60: 强 大于等于 50: 一般 大于等于 25: 弱 大于等于0: 非常弱

对应输出为:

VERY_WEAK, WEAK, AVERAGE, STRONG, VERY_STRONG, SECURE, VERY_SECURE

样例输入

dgsayd$%12

样例输出

SECURE

function verify(pwd){
    let score = 0;
    // 一、密码长度:
    if(pwd.length<=4) score+=5; //   5 分: 小于等于4 个字符 
    if(pwd.length>=5&&pwd.length<=7) score+=10; //   10 分: 5 到7 字符 
    if(pwd.length>=8) score+=25; //   25 分: 大于等于8 个字符 
		
    // 二、字母: 
    let letter = /[a-zA-Z]/.test(pwd);
    let mixed = false;
    if(/^[a-z]+$/.test(pwd) || /^[A-Z]+$/.test(pwd)) score+=10	//   10 分: 全都是小(大)写字母 
    if(/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) {  //   20 分: 大小写混合字母 
        score+=20;
        mixed = true;
    }
		
    // 三、数字:
    let number = /\d/.test(pwd);
    if(pwd.match(/\d/g).length == 1) score+=10;  //   10 分: 1 个数字
    if(pwd.match(/\d/g).length > 1) score+=20;	 //   20 分: 大于1 个数字 
		
    // 四、符号:
    let symbol = /[^a-zA-Z0-9]/.test(pwd);
    if(pwd.match(/[^a-zA-Z0-9]/).length == 1) score+=10;  //   10 分: 1 个符号 
    if(pwd.match(/[^a-zA-Z0-9]/).length > 1) score+=25;   //   25 分: 大于1 个符号 
		
    // 五、奖励:
    if(letter && number && !mixed && !symbol) score += 2;  //   2 分: 字母和数字 
    if(letter && number && !mixed && symbol) score += 3;   //   3 分: 字母、数字和符号 
    if(letter && number && mixed && symbol) score += 5;    //   5 分: 大小写字母、数字和符号 
		
    // 最后的评分标准:
    if(score >= 90) console.log("VERY_SECURE"); 	 //   大于等于 90: 非常安全
    if(score >= 80) console.log("SECURE"); 		 //   大于等于80: 安全 ,
    if(score >= 70) console.log("VERY_STRONG"); 	 //   大于等于70: 非常强 ,
    if(score >= 60) console.log("STRONG"); 		 //   大于等于 60: 强 , 
    if(score >= 50) console.log("AVERAGE"); 		 //   大于等于 50: 一般 , 
    if(score >= 25 && score<50) console.log("WEAK");     //   大于等于 25: 弱 , 
    if(score > 0 && score <25) console.log("VERY_WEAK");  //   小于 25: 非常弱 ,
}
 

猜你喜欢

转载自blog.csdn.net/sdasadasds/article/details/107694927