题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
function solution(str){
let score = 0;
// 密码长度
if(str.lenght <= 4){
score += 5
}else if(str.length <= 7){
score += 10
}else {
score += 25
}
let lowerCaseCharNum = 0; //英文小写字母
let upperCaseCharNum = 0; //英文大写字母
let numberNum = 0; //数字个数
let otherNum = 0; //符号
// 每种字符可能的长度
for(let i=0; i<str.length; i++){
let char = str.charAt(i)
if(/[a-z]/.test(char)){
lowerCaseCharNum++;
}else if(/[A-Z]/.test(char)){
upperCaseCharNum++
}else if(/[1-9]/.test(char)){
numberNum++;
}else if(isOtherChar(char)){
otherNum++;
}
}
// 字母
if(!lowerCaseCharNum && !upperCaseCharNum){
score += 0
}else if(lowerCaseCharNum && upperCaseCharNum){
score += 20
}else{
score += 10
}
// 数字
if(numberNum === 1){
score += 10
}else if(numberNum > 1){
score += 20
}
// 符号
if(otherNum === 1){
score += 10
}else if(otherNum > 1){
score += 25
}
// 奖励
if(lowerCaseCharNum && upperCaseCharNum && numberNum && otherNum){
score += 5
}else if((lowerCaseCharNum || upperCaseCharNum) && numberNum && otherNum){
score += 3
}else if((lowerCaseCharNum || upperCaseCharNum) && numberNum){
score += 2
}
if(score>=90){
return 'VERY_SECURE'
}else if(score>=80){
return 'SECURE'
}else if(score>=70){
return 'VERY_STRONG'
}else if(score>=60){
return 'STRONG'
}else if(score>=50){
return 'AVERAGE'
}else if(score>=25){
return 'WEAK'
}
return 'VERY_WEAK'
}
// 判断是否是其他字符
function isOtherChar(char){
let num = parseInt(char.charCodeAt(0))
if(num >= 0x21 && num <= 0x2F){
return true
}else if(num >= 0x3A && num <= 0x40){
return true
}else if(num >= 0x5B && num <= 0x60){
return true
}else if(num >= 0x7B && num <= 0x7E){
return true
}
return false
}
let line
while(line=readline()){
console.log(solution(line))
}