Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript switch using intervals

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";
            }

???

like image 327
FernandoSBS Avatar asked Jun 08 '10 00:06

FernandoSBS


3 Answers

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";
        }
like image 188
Cahit Avatar answered Oct 04 '22 18:10

Cahit


No. switch can be used only with discrete values. For ranges you'll have to use an if statement.

var val = parseInt(troops[i]);
if (val < 10) {
    editbox.style.fontSize = "13px";
} else if (val < 100) { 
    // ...
} else {
}
like image 42
VoteyDisciple Avatar answered Oct 04 '22 18:10

VoteyDisciple


Sometimes a switch is too much like work

var j= parseInt(troops[i]),
val= j<10? 13: j<100? 12: j<1000? 8: 10;

editbox.style.fontSize= val+'px';
if(val== 8) editbox.size= 3;
like image 25
kennebec Avatar answered Oct 04 '22 18:10

kennebec