javascript - Increment variable based on global variable -
what want following code
$floorno = 1; // current floor if ($floorno == 1){ $number1++; } else if ($floorno == 2){ $number2++; } ...
etc.
is there shorter version, ($number+$floorno)++;
?
you use array:
var arr = [1, 2, 3]; var floorno = 1; arr[floorno]++; // = [1, 3, 3]
if floorno
entered user, can use modulo operator ensure it's valid:
arr[floorno % arr.length]++; // = [1, 3, 3]
Comments
Post a Comment