Mar 24, 2013

Javascript end loop wrapper

I was taking an HTML5 Game Development course and saw a neat trick for wrapping at the end of a sequence:

index = index + 1 % array.length;
It saves 3 lines of code, the usual:
index++
if (index > array.length) {
index = 0;
}

No comments:

Post a Comment