题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while (line = await readline()) {
let r = isPs(line);
console.log(r)
}
}()
function isPs(password) {
//48 - 57 数字
//97 - 122 小写字母
//65 - 90 大写字母
// 32 空格 10 换行
if (password.length < 9) {
return 'NG'
}
let hasNumber, hasLowerChar, hasUpperChar, hasOther;
for (let i = 0; i < password.length; i++) {
const code = password.charCodeAt(i);
if (48 <= code && code <= 57) {
hasNumber = true
continue;
}
if (97 <= code && code <= 122) {
hasLowerChar = true
continue;
}
if (65 <= code && code <= 90) {
hasUpperChar = true
continue;
}
if (code != 32 && code != 10) {
hasOther = true
}
}
const num = [hasNumber, hasLowerChar, hasUpperChar, hasOther].reduce((count, i) => {
count += (i ? 1 : 0);
return count
}, 0)
if (num < 3) {
return 'NG'
}
for (let i = 0; i < password.length - 6; i++) {
for (let j = i + 3; j < password.length - 3; j++) {
if (password.substring(i, i + 3) === password.substring(j, j + 3)) {
return 'NG'
}
}
}
return 'OK'
}

查看9道真题和解析