题解 | 简单密码

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    const word = await readline();
    let newWord = '';
    for(let char of word) {
        newWord += charToCode(char);
    }
    console.log(newWord);
}()

function charToCode(char) {
    const charCode = char.charCodeAt(0);
    if(/[a-z]/.test(char)) {
        if(charCode <= 111) {
            return Math.floor((charCode - 97)/3) + 2;
        }
        else if(charCode <= 115) return 7;
        else if(charCode <=118) return 8;
        else return 9;
    }
    if(/[A-Z]/.test(char)) {
        if(char === 'Z') return 'a';
        const lowerCase = char.toLowerCase(), lowerCaseCode = lowerCase.charCodeAt(0);
        return String.fromCharCode(lowerCaseCode + 1);
    }
    if(/[0-9]/.test(char)) return char;
    return char;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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