Modulus-based index for cycling array values

Ah! I forgot about using modulus operator as array index for cycling through array values! Thanks for the reminder, Angular docs (One Time Binding example).

Note the relevant bit:

$scope.name = names[counter % names.length];

Normally I think I’d do something dumb like:

  var counter = 0;
  var names = ['Igor', 'Misko', 'Chirayu', 'Lucas'];

  $scope.clickMe = function(clickEvent) {
    $scope.name = names[counter];
    if (++counter >= names.length ) {counter = 0;}
  };

Which maybe I guess isn’t super dumb, but I like the elegance of the Angular example, as it does away with the minor-but-present cognitive overhead of “do I greater-than/equal this, or just greater than, or pre-increment, post-increment, subtract one from the array length?” mess.

Also, in my version, you lose the counter’s true value, as it becomes more of a cycle counter.

ColourLovers API mini-project

colors.telecommutetojuryduty.com

Built using Angular, Bootstrap, and open access to the ColourLovers.com api, check it out! It’s a simple way to view the most popular patterns, palettes, and individual colors from ColourLovers.com.

This was a super old site I built long, long ago, with some pretty scrappy jQuery and gross jQuery markup templates, but it felt like a good thing to upgrade now that I’m learning Angular. Plenty of features I’d like to add, but for now, it’s mostly just a quick n’ easy way to browse some nice tones.

Source code is available to peruse here.