The following question uses if else to achieve different conditions (changes in add_step). The result value of add_level is different:
var add_level = 0;
if(add_step == 5){
add_level = 1;
}
else if(add_step == 10) {
add_level = 2;
}
else if(add_step == 12){
add_level = 3;
}
else if(add_step == 15){
add_level = 4;
}
else {
add_level = 0;
}
1) The above functions can also be achieved through switch:
var add_level = 0;
switch(add_step){
case 5 : add_level = 1;
break;
case 10 : add_level = 2;
break;
case 12 : add_level = 3;
break;
case 15 : add_level = 4;
break;
default : add_level = 0;
break;
2) Javasctipt is implemented through || and &&:
var add_level = (add_step==5 && 1) || (add_step==10 && 2) || (add_step==12 && 3) || (add_step==15 && 4) || 0;
3) The second way of writing can also be abbreviated as:
var add_level={'5':1,'10':2,'12':3,'15':4}[add_step] || 0;
A basic formula can be derived from the second way of writing:
add_step==5 && add_level=1
is equivalent to <==>
if(add_step==5){
add_level = 1
}