HTML and CSS Reference
In-Depth Information
with a formula, named after himself, to answer the rabbit question. The sequence starts
with 0 and 1, and then produces the next number by adding the two previous numbers
together; so 0 + 1 is 1, 1 + 1 is 2, 1 + 2 is 3, 2 + 3 is 5, and so on.
In Example 7.10, the Fibonacci sequence recurses 20 times and as you can see, if
these numbers represent rabbits, we have over 4,000 rabbits in a short period of time!
EXAMPLE 7.10
<html>
<head><title="Fibonacci Series"</title>
<script type="text/javascript">
1
var count = 0;
2
function fib(num){
3
count++;
switch(num){
case 0 :
4
return(0);
break;
case 1:
5
return(1);
break;
default:
6
return(fib(num - 1) + fib( num - 2 )) ;
break;
}
}
</script>
</head>
<body>
<big>
<div align = "center">
<table border="1">
<tr>
<script type = "text/javascript">
7
for( n=0; n < 20; n++){
8
value = fib(n);
document.write("<td>" + value + "</td>");
}
9
alert("Function called itself "+ count + " times!!");
</script>
</tr>
</table>
</div>
</big>
</body>
</html>
Search WWH ::




Custom Search