Copy the code over here! http://betacoding.net/javascript/javascript-programming-fibonacci-sequence-with-the-for-statement
__________________
Thank you for watching this video, if you like it please don’t forget to like it,
or subscribe to my channel for more programming tutorials
—
also visit my website for more content
http://betacoding.net
Original source
12 responses to “Javascript Tutorial – fibonacci sequence with For statement”
Good
Wow! The video is very satisfying and well explained
Try with this very simple and results will display in horizontal…I modified above betacoding
var fibonacci_series = function (n)
{
var a=0,b=1,f=b,s =[0,1];
for(var i=0;i<n;i++) {
f= a+b;
s.push(f);
a=b;
b=f;
}
return s;
};
fibonacci_series(n)
Can someone explain to me why I can't get my numbers vertical instead of horizontal
Isn't 0 considered fibonacci number? If you replace a=0 to a=1 and b=1 to b=0 it will print all numbers from 0.
var a, b, result;
a = 1;
b = 0;
result = b;
for (var i=1; i<=20; i++) {
document.write(result+"<br/>");
result = a + b;
a = b;
b = result;
}
I feel like my version is a bit better(and simpler).
<script>
var a= 0;
var b = 1;
var c= 0;
while(c <= 121565){
document.write(c);
document.write("<br/>");
c = a + b;
a = b;
b =c;
}
</script>
Thanks!
good job! thanks!
very easy and awesome
this would be a much better approach 🙂
var fib = function(num){
var x = 1;
for(i=0, j=1, k=1; k <= num; i=j,j=x,k++){
console.log(x);
x = i+j;
}
}
Not working :
if you ask for results over a 100, numbers start showing incorrectly.
if you ask for result over 1478, numbers are replaced by the word "infinity".
… No use, but nice try though !
For the ones who liked the editor, it's Sublime Text.