A typical question asked in "History of JavaScript" classes where the teacher mistakenly believes they are teaching programming.

Write a JavaScript program that accepts two numbers as input from the user and computes the sum of all integers between those two numbers including the two inputs. For example, if the user input 3 and 6, then the answer would be: 18 (3+4+5+6).

This is another question where a loop is expected as the answer. Unfortunately a loop is the least efficient way to get the answer to this question. The answer to this question can be obtained using two additions, one subtraction, one multiplication and one division without any loops.

As we have already covered the difference between how you should be collecting input from a form rather than using prompt to collect the inputs in a prior comparison, here we'll just look at the function for calculating the answer. We'll also assume that the first number is always smaller than or equal to the second (if they are not you'd simply swap them before calling the function).

`function sumRange(x, y) {`

var result = 0;

for (var i = x; i <= y; i++) {result += i;}

return result;

}

There isn't a lot wrong with this answer when you consider that the question is basically asking you to use a loop. Most of the outdated code they would be expecting would be in the code that calls the function and which processes the result.

The mistake here is not a coding one, it relates to the other half of programming that many courses forget to teach - the part related to creative problem solving. Apply a little of this to this particular problem and you can easily see that no loop is required in order to add up all the numbers in the range, the total can be calculated directly from the end points of the range. Now this wouldn't be an issue if the inputs are 3 and 6 but suppose they are -3000000 and 600000000000. The straightforward loop ends up being extremely inefficient for solving this particular problem once the numbers get further apart.

`sumRange = function(x, y) {`

return (y-x+1)/2*(x+y);

}

This function returns the exact same answer for any values of x and y where x is less than or equal to y as the above function does. It performs exactly five arithmetic operations each time regardless of the distance between the two numbers where the prior version needs to perform two arithmetic operations and a comparison for every number in the range (so that the 3 to 6 example performs eight arithmetic operations and four comparisons) so you can easily see that this solution that doesn't use a loop becomes more efficient as soon as the range includes more than one number.

This solution is not only a lot more efficient, it also uses two fewer variables to perform the calculation.

**Note** When I came across this particular homework question I immediately realised that the answer would not require a loop. It then took a few minutes for me to work out the calculation required to work out this answer without a loop. I did not know the formula for adding sequential numbers before writing this page and did not have anything that told me that such a formula existed and I didn't look it up anywhere. I simply used creative problem solving to recognise from the question that such a solution exists and then proceeded to work out on paper what that solution would be before rewriting it into JavaScript. Of course once you have the solution worked out you then can easily see (by rearranging the code) that you are simply multiplying the average value by the number of integers and that the answer must therefore always be correct.

You can even cater for the two numbers being entered in either order by wrapping the y-x inside Math.abs().

*This article written by Stephen Chapman, Felgall Pty Ltd.*