I came across an interesting JavaScript homework question just recently where the person was asking which type of loop was best used for the solution.

The question was to accept two integers and then add all of the integers between them (inclusive) together and display the result. (eg. 3 and 6 returns 18)

Of course the correct answer is that the solution to this question doesn't require a loop at all.

Assuming that x<=y the following function calculates the answer without actually adding up all the separate numbers.

So if for example you call sumRange(11,10000) which returns 50004945 which is what you get when you add 11+12+13+14+...+9999+10000 togetherCode:sumRange = function(x, y) {return (y-x+1)/2*(x+y);}

For any calculations where there are patterns to the numbers (in this case the numbers being sequential integers) there is always a formula that can be applied so as to avoid the need for a loop.

Probably the most common place where calculations involving sequential integers occurs is with dates.