PDA

View Full Version : recursive fibanici

akshay.chugh
09-30-2012, 10:08 PM

<title>a</title>
<script text="javascript/text">
document.write("the fibanic series is as follows" )
var i;
for (i=0; i<6; i++){ document.write(i);
value=i;
a(value);
}
function a(value){if(value===0){
document.write("1");}
if(value===1){
docuument.write("1");}
else
document.write(a(value-1)+a(value-2));}

</script>
<body></body>
</html>
i used the below written code but the the result that is coming is :
the fibanic series is as follows00

Old Pedant
09-30-2012, 11:58 PM
HINT: There is a HUGE difference between RETURNING a value from a function and doing document.write() of the value inside the function.

VIPStephan
10-01-2012, 12:15 AM
And it’s called Fibonacci (https://en.wikipedia.org/wiki/Fibonacci_number), not fibanici.

AndrewGSW
10-01-2012, 12:25 AM
docuument

Philip M
10-01-2012, 08:10 AM
<script text="javascript/text">

<script type = "text/javascript">

var r = [1], n = 1, a = 0, b = 1, next;
function nextFibonacci() {
next = a + b;
return b = (a = b, next);
}
while (n++ < 12) {
r.push(nextFibonacci());
}

alert (r); // 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
document.write(r);

</script>

Or simpler:-

<script type = "text/javascript">

var fib = [];
fib[0]=1;
fib[1]=1;
var numFib = 12; // calculate first numFib numbers
for (var c=2; c<=numFib; c++) {
fib[c] = fib[c-1] + fib[c-2];
}
alert ("First " + numFib + " Fibonnacci numbers:- " + fib);
document.write("First " + numFib + " Fibonnacci numbers:- " + fib);

</script>