When reviewing, I sometimes encounter this kind of loop:
i = begin
while ( i != end ) {
// ... do stuff
if ( i == end-1 (the one-but-last element)
I know I've seen this when people tried to join elements of an array into a comma-seperated string:
for(i=0;i0) {
string += ','
}
string += elements[i]
}
You either have that if clause in there, or you have to duplicate the string += line again at the end.
The obvious solution in this case is
string = elements.join(',')
But the join method does the same loop internally. And there isn't always a method to do what you want.