题解 | #密码验证合格程序#
密码验证合格程序
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 validLength = checkLength(line);
let validType = checkType(line);
let validDup = checkDup(line);
if (validLength && validType && validDup) {
console.log("OK");
} else {
console.log("NG");
}
function checkLength(line){
//长度校验
return line.length > 8;
}
function checkType(line) {
//包含四种中的三种
let occurance = 0;
if (/[a-z]/.test(line)) {
occurance++;
}
if (/[A-Z]/.test(line)) {
occurance++;
}
if (/[0-9]/.test(line)) {
occurance++;
}
if (/[^0-9a-zA-Z]/.test(line)) {
occurance++;
}
return occurance >= 3;
}
function checkDup(line) {
//不含子串重复
let pieces = [];
for (let i = 0; i < line.length - 2; i++) {
let subStr = line.slice(i, i + 3);
if (pieces.includes(subStr)) {
return false;
} else {
pieces.push(subStr);
}
}
return true;
}
}
})();
查看5道真题和解析