Algorithm that I had to write is a perfect case for while, or do..while loop, however I found out that if I will implement it with a for loop I will save few lines of code and also scope of variables will be more appropriate. Take a look at the following code:
for ( var i = 1, offset = -1; currentPosition.top === fakePosition.top && offset !== 0; i++ ) {
fakePosition.top -= i * 10;
offset = this.documentView.getOffsetFromPosition( fakePosition );
fakePosition = this.documentView.getRenderedPosition( offset );
}
As you can see second parameter is very atypical as for for loops. My mine reason to switch to this loop was fact that I need iterator (i) and offset variables inside loop (and only there).
What do you think about this approach?