题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const lines: string[] = [];

rl.on("line", function (line: string) {
    lines.push(line);
});

rl.on("close", () => {
    lines.forEach((item) => console.log(isLegalPassword(item)));
});

const isLegalPassword = (password: string): string => {
    // 1.长度超过8位
    if (password.length <= 8) {
        return "NG";
    }

    // 2.包括大小写字母.数字.其它符号,以上四种至少三种
    if (/' '/.test(password)) {
        return "NG";
    }
    const types = [
        /[A-Z]/.test(password),
        /[a-z]/.test(password),
        /[0-9]/.test(password),
        password.replace(/[0-9a-zA-Z]/g, "").length > 0,
    ];
    if (types.filter((item) => item === true).length < 3) {
        return "NG";
    }

    // 3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
    const childrenStr: string[] = [];
    for (let i = 0; i < password.length; i++) {
        const childStr = password.substring(i, i + 3);
        if (childrenStr.includes(childStr)) {
            return "NG";
        } else {
            childrenStr.push(childStr);
        }
    }

    return "OK";
};

全部评论

相关推荐

一表renzha:你点进去没打招呼他也会有提示的,之前我点进美的,还没打招呼,他马上给我发了不太合适哦
点赞 评论 收藏
分享
mama3925:建议专业技能里测试移到最上面,加粗。然后适当加入些自动化测试工具。第二个项目,第三条亮点最后错别字。然后佬如果对自己很自信的话,可以项目放前面,然后项目里可以编造点测试经历,写在写在项目亮点的前两行。最后可加个自我评价,放个博客或者仓库链接
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务