简化长开关语句 [英] Simplifying Long Switch Statements

查看:76
本文介绍了简化长开关语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to brush up on my javascript because it is my weakest language, so I thought "Hey lets make a simple 'translating' program to test my skills". Well I was able to make it translate one way so far(I havent worked on untranslating the stuff people input), but anyway the way it does it is by a series of many cases inside a switch. Im wondering if there is anyway I can simplify the code instead of having a million switch cases. Thanks here is my code.

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;

            };      


推荐答案

您可以使用具有

You could use an object with properties like

{
    a: 'z',
    b: 'y',
    c: 'x',
    // ...
    z: 'a'
}

使用默认值 ina [i]

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

这篇关于简化长开关语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆