Fix JS error when computing tab wrapping.

When computing tab wrapping behavior, these check expressions will error at the boundary (when i is 0 or is at the last index). This patch checks i at these boundaries before it performs the other check.