javascript switch using intervals

FernandoSBS picture FernandoSBS · Jun 8, 2010 · Viewed 8.2k times · Source

Can I use intervals in a switch statement?

Like

switch (parseInt(troops[i])) {
                case <10:
                    editbox.style.fontSize = "13px";
                    break;
                case <100:
                    editbox.style.fontSize = "12px";
                    break;
                case <1000:
                    editbox.style.fontSize = "8px";
                    editbox.size = 3;
                    //editbox.style.width = "18px";
                    break;
                default:
                    editbox.style.fontSize = "10px";
            }

???

Answer

Cahit picture Cahit · Jun 8, 2010

This should work though:

var j = parseInt(troops[i]);
switch (true) {
            case (j<10):
                editbox.style.fontSize = "13px";
                break;
            case (j<100):
                editbox.style.fontSize = "12px";
                break;
            case (j<1000):
                editbox.style.fontSize = "8px";
                editbox.size = 3;
                //editbox.style.width = "18px";
                break;
            default:
                editbox.style.fontSize = "10px";
        }