题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
这应该算是比较简洁的一个版本了吧 欢迎有更好方法的朋友指正 完整代码如下:
let res = 0;
while(line = readline()) {
line.length > 8 ? res += 25 : line.length > 4 ? res += 10 : res += 5;
line.match(/(?=.*[a-z])(?=.*[A-Z])^.+$/) ? res += 20 : res += 10;
//下面这两步我一直想去掉第一个条件,但是一旦去掉系统就会报错‘Can't read property 'length' of null’;没办法只能加上了
line.match(/\d/g) ? (line.match(/\d/g).length > 1 ? res += 20 : res += 10) : res = res;
line.match(/\W/g) ? (line.match(/\W/g).length > 1 ? res += 25 : res += 10) : res = res;
//这个也是相对来说比较简洁的方法了
line.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)^.+$/) ? res += 5
: line.match(/(?=.*\d)(?=.*[A-Za-z])(?=.*\W)^.+$/) ? res += 3
: line.match(/(?=.*\d)(?=.*[A-Za-z])^.+$/) ? res += 2
: res = res;
}
//最后采用了一个for循环来显示结果
let arr1 = [90, 80, 70, 60, 50, 25, 0];
let arr2 = ['VERY_SECURE', 'SECURE', 'VERY_STRONG', 'STRONG', 'AVERAGE', 'WEAK', 'VERY_WEAK'];
for (let i = 0; i < 7; i++) {
if (res >= arr1[i]) {
console.log(arr2[i]);
break;
}
}


查看14道真题和解析