So I was trying to make a simple Fibonacci generator in Javascript, but it doesn’t seem to be working! I tried it both in my browser (Chrome) as well as JSFiddle, still doesn’t work. Anyone have an idea as to what is going on here? It seems as if it isn’t even entering the function, since no alert pops up.
<html>
<head>
<script type='text/javascript'>
function fib()
{
alert('In fib function');
var c = parseInt(document.getElementById('f').value, 10);
var o = document.getElementById('o');
var x = 0;
var y = 1;
var z = 0;
for (i = 0; i < 0; i++)
{
o.innerHTML += '\n' + x;
z = x + y;
x = y;
y = z;
}
return false;
}
</script>
</head>
<body>
<form onsubmit='return fib()'>
<input id='f' type='text'></input>
<input type='button' value='Submit' onClick='return fib()'
<pre id='o'></pre>
</body>
</html>
There are two problems with your code. First, as David pointed out, you won’t actually print out any numbers because the loop will never execute. Secondly, you forgot to close your first
<input>tag, which consumed the<pre>tag, which is where you were trying to deliver your output. See this updated fiddle with a working output:http://jsfiddle.net/6neQU/