题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
console.log(judge(line) ? 'OK' : "NG")
});
const judge = (input) => {
if (input.length < 8) return false
if (!checkTextType(input)) return false
if (findReduntSubstring(input)) return false
return true
}
const checkTextType = (input) => {
const tmp = {
upperChar: null,
lowerChar: null,
num: null,
symbol: null
}
input.split('').forEach((c) => {
if (c >= 'a' && c <= 'z') tmp['lowerChar'] = true
else if (c >= 'A' && c <= 'Z') tmp['upperChar'] = true
else if (c >= '0' && c <= '9') tmp['num'] = true
else tmp['symbol'] = true
})
return Object.values(tmp).filter(Boolean).length >= 3
}
const findReduntSubstring = (input) => {
for(let i = 0; i <= input.length - 3; i++) {
const text = input.substring(i, i+3)
if (input.indexOf(text) !== input.lastIndexOf(text)) return true
}
return false
}

