题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const ScoreList = [ { maxScore: 95, minScore: 90, output: "VERY_SECURE" }, { maxScore: 89, minScore: 80, output: "SECURE" }, { maxScore: 79, minScore: 70, output: "VERY_STRONG" }, { maxScore: 69, minScore: 60, output: "STRONG" }, { maxScore: 59, minScore: 50, output: "AVERAGE" }, { maxScore: 49, minScore: 25, output: "WEAK" }, { maxScore: 24, minScore: 0, output: "VERY_WEAK" }, ]; const getScore = (str: string): number => { let score = 0; const len = str.length; let hasNum = false; let hasLowLetter = false; let hasUpperLetter = false; let hasSymbol = false; // 一、密码长度: if (len <= 4) { score += 5; } else if (len >= 5 && len <= 7) { score += 10; } else { score += 25; } // 二、字母: if (/[a-z]/.test(str)) { hasLowLetter = true; score += 10; } if (/[A-Z]/.test(str)) { hasUpperLetter = true; score += 10; } // 三、数字: const numCount = len - str.replace(/[0-9]/g, "").length; if (numCount === 1) { hasNum = true; score += 10; } else if (numCount > 1) { hasNum = true; score += 20; } // 四、符号: const symbolCount = len - str.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g, '').length; if (symbolCount === 1) { hasSymbol = true; score += 10; } else if (symbolCount > 1) { hasSymbol = true; score += 25; } // 五、奖励(只能选符合最多的那一种奖励) if (hasLowLetter && hasUpperLetter && hasNum && hasSymbol) { score += 5; } else if ((hasLowLetter || hasUpperLetter) && hasNum && hasSymbol) { score += 3; } else if ((hasLowLetter || hasUpperLetter) && hasNum && !hasSymbol) { score += 2; } return score; }; rl.on("line", function (line) { const score = getScore(line); const output = ScoreList.find((item) => score >= item.minScore && score <= item.maxScore).output; console.log(output); });