-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
Like many other programming languages, Javascript allows to code recursive functions.
A recursive function calls/iterates itself until it reaches a result.
A recursion should be used carefully, because it could exhaust system resources.
One of its use is for traversing the nodes of complex or non-linear data structures.
A code structure for a recursive function in Javascript could look like as follows:
function recursiveFunction(anyNumber)
{
if(anyNumber === 0)
{
console.log(anyNumber);
}
else
{
console.log(anyNumber);
recursiveFunction(--anyNumber);
}
}
recursiveFunction(5);
The code above is decrementing a given number until it reaches 0.
It is important to note that the following call of a function in Javascript should not work:
var f = function Any()
{
alert("Hello");
}
//Shouldn´t work! - Uncaught ReferenceError
Any();
The code should look like as follows, if a call by name is wanted
function Any()
{
alert("Hello");
}
//Should work!
Any();
The user interaction part should look like the content as seen below by starting "index.html" in a web browser.
The binary tree will be traversed with the help of a recursive function. The User decides which knot should be searched for. There is also a broken version of the searching which visualizes an Uncaught ReferenceError.
Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman