Javascript Recursion

Recursion is when a function calls itself. This is often useful in mathematics,
such as fi nding the nth number in the Fibonacci series (1, 2, 3, 5,
8, 13, 21…).
function fi bonacci(n) {
if ( n < 2 ) {
return 1;
} else {
return fi bonacci(n-2) + fi bonacci(n-1);
}
}
fi bonacci(5);
8
fi bonacci(10);
89
Related Posts:
  • JavaScript Interview Questions JavaScript Interview Questions What is the difference between == and === ? The == checks for value equality, but === checks for both type and value. difference between innerHTML and append() in JavaScript? InnerHTML is… Read More
  • JavaScript Timers setTimeout() and setInterval() allow you to register a functionto be invoked once or repeatedly after a specified amountof time has elapsed. These are important global functions ofclient-side JavaScript, and are therefore de… Read More
  • WebSocket() constructor socket with the WebSocket() constructor:var s = new WebSocket("ws://ws.example.com/resource");The argument to the WebSocket() constructor is a URL that usesthe ws:// protocol (or wss:// for a secure connection like thatused … Read More
  • Include Inline JavaScript code To include inline JavaScript code, place JavaScript code inside the <script> element directly, as follows: <script type=”text/javascript”> function sayHi(){ alert(“Hi!”); } </script> When using inline Java… Read More
  • Retrieving the Response A complete HTTP response consists of a status code, a set ofresponse headers, and a response body. These are availablethrough properties and methods of the XMLHttpRequestobject:• The status and statusText properties return t… Read More
  • var keyword JavaScript The var and function are declaration statements—they declare or define variables and functions. These statements defineidentifiers (variable and function names) that can be used elsewherein your program and assign values to … Read More
  • concat() Method The concat() method returns the array resulting from appending its arguments to  the array on which it was invoked. Given the script: var myArray = ["red", "green", "blue"]; alert(myArray.concat("cyan", "yello… Read More
  • HTTP “200 OK” response A complete request is not necessarily a successful request, andyour handler for the load event should check the status codeof the XMLHttpRequest object to ensure that you received anHTTP “200 OK” response rather than a “404 … Read More
  • Javascript cookie string We can set a cookie by setting document.cookie to a cookie string.  The following code will set a cookie with the UserName set as Paul,  and an expiration date of 28 December 2010. <html> <head> <… Read More
  • Expression Statements Assignment statements are one major category ofexpression statements.  For example: greeting = "Hello " + name;i *= 3; The increment and decrement operators, ++ and --, are relatedto assignment statements. These have … Read More
  • Prototypes Every Java-Script object has a second JavaScript object (or null, but thisis rare) associated with it. This second object is known as aprototype, and the first object inherits properties from theprototype.All objects created… Read More
  • JavaScript Security Downloading and running programs written by unknown parties is a dangerous proposition. A program available on the Web could work as advertised, but then again it could also install spyware, a backdoor into your system, … Read More
  • Reserved Words in JavaScript Reserved Words in JavaScript 1.5 >abstract else instanceof switch >boolean enum int synchronized >break export interface this byte extends long throw case f… Read More
  • javascript Date, Time Best seo practices How to promoting webpages Top seo tips top 10 e-commerce tips Additional seo tips Advanced seo tips Mobile seo tips Javascript Date The methods getDate(), getDay(), getMonth(), and g… Read More
  • Creating a Cookie-Javascript To make life easier for ourselves, we'll write a function that allows us to create a new cookie and set certain of its attributes with more ease. We'll look at the code first and create an example using it shortly. fun… Read More