javascript - A for loop that does the same as if else statement? -


i have if else statement, wondering if change loop or similar.

if (lastscroll >= 0 && lastscroll < 40) {     pos = 0; } else if (lastscroll >= 40 && lastscroll < 80) {     pos = 1; } else if (lastscroll >= 80 && lastscroll < 120) {     pos = 2; } else if (lastscroll >= 120 && lastscroll < 160) {     pos = 3; } else if (lastscroll >= 160 && lastscroll < 200) {     pos = 4; } else if (lastscroll > 200) {     pos = 5; } 

i want change because there on 100 positions. thinking creating loop this:

var = 0; greater = 0; less = 40; (i = 0; < 100; i++) {     if (lastscroll >= greater && lastscroll < less) {         pos = i;         greater += 40;         less += 40;     }  } 

the if else statement works don't want create 100 if else statements. wrapped in scroll function.

because it's linear can use division , rounding

pos = math.floor(lastscroll / 40); if (pos > 5) pos = 5; 

Comments