Cycling between element above and below current array element
First off, your code was a bit difficult to read, because you combined the algorithm you need with your functionality, that you didn't explain. It's not only easier for people helping you, but also yourself, if you try to separate your problem from the application.

Your attempt was to calculate an index from the previous index. I instead created a function, that mapped a increasing index (0, 1, 2, 3, 4,...) to the targets (0, 1, -1, 2, -2,...). So we are looking for a function f(i) where f(0) = 0, f(1) = 1, f(2) = -1, f(3) = 2, etc.

I came up with: (i % 2 ? 1 : -1) * Math.floor( (i+1) / 2 ). 

Now you just need a condition to skip new indexes outside your array. I came up with the following:

var yourArray = new Array(10);
var start = 2;

var i = 0;
var max = yourArray.length;

while (max) {
  var newI = (i%2?1:-1) * Math.floor((i+1)/2) + start;
  if (newI &gt;= 0 &amp;&amp; newI &lt; yourArray.length) {
    document.write( newI + "&lt;br&gt;");
    // do something with yourArray[newI];
    max--;
  }
  i++;
}


My problem was how to decide when to stop the loop. For now i just loop over twice the length of the array. That will usually lead to some unnecessary iterations, but maybe someone can come up with a better solution, or there maybe a condition that comes from your application. UPDATE: Found a solution.