GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javascript.info — Sum all numbers till the given one back to the lesson Sum all numbers till the given oneimportance: 5 Write a function For instance:
Make 3 solution variants:
An example of the result:
P.S. Which solution variant is the fastest? The slowest? Why? P.P.S. Can we use recursion to count The solution using a loop: The solution using recursion: The solution using the formula: P.S. Naturally, the formula is the fastest solution. It uses only 3 operations for any number The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it’s slower. P.P.S. Some engines support the “tail call” optimization: if a recursive call is the very last one in the function, with no other calculations performed, then the outer function will not need to resume the execution, so the engine doesn’t need to remember its execution context. That removes the burden on memory. But if the JavaScript engine does not support tail call optimization (most of them don’t), there will be an error: maximum stack size exceeded, because there’s usually a limitation on the total stack size. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |