According to Google Calculator (-13) % 64
is 51
.
According to Javascript (see this JSBin) it is -13
.
How do I fix this
Though it isn't behaving as you expected, it doesn't mean that JavaScript is not 'behaving'. It is a choice JavaScript made for its modulo calculation. Because, by definition either answer makes sense.
See this from Wikipedia. You can see on the right how different languages chose the result's sign.