简化长开关语句

时间:2016-09-30 22:15:53

标签: javascript

我需要更新我的javascript,因为它是我最弱的语言,所以我想"嘿,让我们做一个简单的翻译'程序来测试我的技能"。好吧,到目前为止我能够将它翻译成一种方式(我还没有翻译过人们输入的东西),但无论如何,它的方式是通过交换机内部的一系列案例。我想知道无论如何我可以简化代码而不是百万个交换机案例。谢谢,这是我的代码。

function main() {
            var get = prompt("Enter what you would like to encode!","At the current time decoding is still a WIP").toLowerCase();
            var ina = [...get];
            for(i = 0; i < ina.length; i++) {
                switch(ina[i]) {
                case "a":
                    ina[i] = "z";
                    break;
                case "b":
                    ina[i] = "y";
                    break;
                case "c":
                    ina[i] = "x";
                    break;
                case "d":
                    ina[i] = "w";
                    break;
                case "e":
                    ina[i] = "v";
                    break;
                case "f":
                    ina[i] = "u";
                    break;
                case "g":
                    ina[i] = "t";
                    break;
                case "h":
                    ina[i] = "s";
                    break;
                case "i":
                    ina[i] = "r";
                    break;
                case "j":
                    ina[i] = "q";
                    break;
                case "k":
                    ina[i] = "p";
                    break;
                case "l":
                    ina[i] = "o";
                    break;
                case "m":
                    ina[i] = "n";
                    break;
                case "n":
                    ina[i] = "m";
                    break;
                case "o":
                    ina[i] = "l";
                    break;
                case "p":
                    ina[i] = "k";
                    break;
                case "q":
                    ina[i] = "j";
                    break;
                case "r":
                    ina[i] = "i";
                    break;
                case "s":
                    ina[i] = "h";
                    break;
                case "t":
                    ina[i] = "g";
                    break;
                case "u":
                    ina[i] = "f";
                    break;
                case "v":
                    ina[i] = "e";
                    break;
                case "w":
                    ina[i] = "d";
                    break;
                case "x":
                    ina[i] = "c";
                    break;
                case "y":
                    ina[i] = "b";
                    break;
                case "z":
                    ina[i] = "a";
                    break;
                default:
                    ina[i] = ina[i]
                    };
                };
                var outa = ina.join("");
                document.getElementById("output").innerHTML = outa;

            };      

4 个答案:

答案 0 :(得分:3)

您可以使用具有

等属性的对象
{
    a: 'z',
    b: 'y',
    c: 'x',
    // ...
    z: 'a'
}

使用默认值ina[i]

ina[i] = object[ina[i]] || ina[i];

答案 1 :(得分:1)

您可以使用几个字符串变量来映射字母。

function translateLetter(input) {
    const untranslated = "abcdefghijklmnopqrstuvwxyz";
    const translated   = "zyxwvutsrqponmlkjihgfedcba";

    var i = untranslated.indexOf(input);
    return translated[i];
}

答案 2 :(得分:1)

您正在使用的abreviation(arr[i],j-1); 具有可以通过简单数学直接实现而无需switch的逻辑(我相信大多数现代JS解释器应该JIT远离实际的方法调用,如果这是一个热循环,所以那里的成本应该是微不足道的):

switch

答案 3 :(得分:0)

只需对ASCII字符代码进行数学运算:

function main() {
    var get = prompt("Enter what you would like to encode!","At the current time decoding is still a WIP").toLowerCase();
    var ina = [...get];

    for (i = 0; i < get.length; i++) {
        var charNum = get.charCodeAt(i) - 96;

        if (charNum > 0 && charNum < 27) {
            ina[i] = String.fromCharCode((27 - charNum) + 96);
        }
    };

    var outa = ina.join("");
    document.getElementById("output").innerHTML = outa;
};