Each edge of the spiral is of square longer than the preceding one. A simple loop draws the edges, increasing the length in each iteration:

var MAX_LENGTH = 20;
var d = 1;

while (d < MAX_LENGTH) {
  walk (d);
  kara.turnRight();
  d ++;
}