I’m incrementing a given value in cycle limit of say 6. I’m curious how we can make this code shorter.
function Cycle_Value(inc_value:Number = 5, times:Number = 3):Number
{
var return_value:Number = inc_value;
while(times >= 1)
{
if(inc_value < 6)
{
inc_value++;
return_value = inc_value;
}
else if(inc_value == 6)
{
return_value = 1;
inc_value = 1;
}
else
trace(inc_value+" is out of bounds");
times--;
}
return return_value;
}
Sample Output: Cycle_Value(5,2) = 1, Cycle_Value(6,10) = 4
Try this:
You can find more information about the modulo operator at: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#modulo
Note 1: If you were willing to work with numbers in the range 0 .. 5 rather than 1 .. 6, you could have simply used the following expression
Note 2: Personally, I would have used different names, e.g. value instead of inc_value and inc instead of times.