Category Archives: raving

JavaScript negative numbers modulus bug

I just got bit by a rather weird bug. In most programming languages you can do “a modulo b” and get back a number between 0..(b-1). This is wonderful when you have an array of n items and you want to select a number in the range of array indexes 0..(n-1) but you want to use some kind of an incremental counter. For example:

var letters = ['a', 'b', 'c', 'd'];
var idx = 0;
for (i = 0; i < 100; i++) {
  window.console.log(letters[idx % letters.length]);
  idx++;
}

This should print out to the debug console: a b c d a b c d a b c d … and so on. The “% letters.length” keeps the range of integer indexes between 0..3 in this case.

So the cool thing is that instead of incrementing idx you could reverse it:

var letters = ['a', 'b', 'c', 'd'];
var idx = 0;
for (i = 0; i < 100; i++) {
  window.console.log(letters[idx % letters.length]);
  idx--;
}

And we should see: a d c b a d c b a d c b a … and so on.

But not in JavaScript.

Apparently there is a bug where it will constrain idx but it will allow negative numbers! So the idx values in this case will go: 0 -1 -2 -3 0 -1 -2 -3 0 -1 -2 -3 … and so on. What we expected was that it would go: 0 3 2 1 0 3 2 1.

The solution (which also ended up on About.com) is to add your modulo back to the number and do a second modulo. It’s totally inefficient, I know. So in this example:

var letters = ['a', 'b', 'c', 'd'];
var idx = 0;
for (i = 0; i < 100; i++) {
  window.console.log(letters[((idx % letters.length) + letters.length) % letters.length]);
  idx--;
}

or the formula: ((a % b) + b) % b)

Ugh.

Locking the Mac OS X dock

Ever accidentally drag an item off the dock? Ever forget what that was and now it’s killing you that you know you might be missing something important? You would think that you can lock your Dock so that things don’t accidentally get removed. It’s such a simple feature—even Windows has had it forever.

Thanks to a quick article on Brighthub, http://www.brighthub.com/computing/mac-platform/articles/43080.aspx, there’s a couple of commands you can type into Terminal to lock it. I took it a step further and just made them into a couple of aliases for my .bash_profile:

alias docklock='defaults write com.apple.dock contents-immutable -bool true;killall Dock'
alias dockunlock='defaults write com.apple.dock contents-immutable -bool false;killall Dock'

So, to lock your Dock now just open the Terminal application and type: docklock

To unlock, type instead: dockunlock

Simple!