## Recommended Posts

Hi people,I need some help. What this is about: Fibonacci numbers, calculation of a Rabbit's population size after x months.There are mainly 2 variables that matter: the number of months (m), and the predicted population size (x).What I'd like to archieve: A table that is being created after the form with months and starting number of rabbits has been filled in.something like this:Month m Population size xMonth m+1 Population size xMonth m+2 Population size xHere's some code I already have:

`function fibonacci() {var b=document.getElementById("rabbitpairs").valuevar m=document.getElementById("months").valuevar r=document.getElementById("var1").valuevar l=document.getElementById("var2").valuevar results = "0   1 ";var n = 0;var x = 1;var i = 0;x = b*xl = l*12p = 1.618033989eval(m);m = m-1;tel = 1 for (j=0; j <= l; j++) {		p = p*p	} for (n=1; n < m; n++) { 		x = x + 0.5*r*i - x/p;	i = x - i;	results = results + (Math.round(x) + "          ");	  }document.write("First " + (m+1) + " numbers from the Fibonnaci Array:<br>" + results +""); }`

As you might see, right now it's writing to a new window [saw it in some sample, don't like it.. that's why I want the table thingie]. The variables r and l do not really matter, they're just a few additions to the calculation. Please take a look at the last bit, where the actual result is being written. I've already read the Try-me tutorial on inserting tables, but I got confused.http://www.w3schools.com/js/tryit.asp?file...table_insertrowthat's the link to the example from w3schools.Could anyone help me please? I really hope so. Best regards,-H

##### Share on other sites

Hi people,I need some help. What this is about: Fibonacci numbers, calculation of a Rabbit's population size after x months.There are mainly 2 variables that matter: the number of months (m), and the predicted population size (x).What I'd like to archieve: A table that is being created after the form with months and starting number of rabbits has been filled in.something like this:Month m Population size xMonth m+1 Population size xMonth m+2 Population size xHere's some code I already have:
`function fibonacci() {var b=document.getElementById("rabbitpairs").valuevar m=document.getElementById("months").valuevar r=document.getElementById("var1").valuevar l=document.getElementById("var2").valuevar results = "0   1 ";var n = 0;var x = 1;var i = 0;x = b*xl = l*12p = 1.618033989eval(m);m = m-1;tel = 1 for (j=0; j <= l; j++) {		p = p*p	} for (n=1; n < m; n++) { 		x = x + 0.5*r*i - x/p;	i = x - i;	results = results + (Math.round(x) + "          ");	  }document.write("First " + (m+1) + " numbers from the Fibonnaci Array:<br>" + results +""); }`

As you might see, right now it's writing to a new window [saw it in some sample, don't like it.. that's why I want the table thingie]. The variables r and l do not really matter, they're just a few additions to the calculation. Please take a look at the last bit, where the actual result is being written. I've already read the Try-me tutorial on inserting tables, but I got confused.http://www.w3schools.com/js/tryit.asp?file...table_insertrowthat's the link to the example from w3schools.Could anyone help me please? I really hope so. Best regards,-H

Try this, and you should be able to adapt it to use the actual input numbers:
`<html><head><script type="text/javascript">function populatetable(m,x,r,p){  var table=document.getElementById('myTable');    for (i = table.rows.length - 1; i >= 0;  i--)	table.deleteRow(i);  for (n=1; n < m; n++)   {	x = x + 0.5*r*i - x/p;	i = x - i;	var row=document.getElementById('myTable').insertRow(0);	var cell0=row.insertCell(0);	var cell1=row.insertCell(1);	cell0.innerHTML=n;	cell1.innerHTML=Math.round(x);  }}</script></head><body><input type="button" onclick="populatetable(5, 1, 20, 3)" value="Populate table"><table id="myTable" border="1"><tr><td>Row1 cell1</td><td>Row1 cell2</td></tr></table></body></html>`

##### Share on other sites

Thank you very much, I will look into it. I'll let you know if it worked.Thanks a bunch

##### Share on other sites

Very cool man! It works!One tiny question: How do I invert the table order? Right now the highest 'month' is on top, and the lowest is on the bottom. I'd like to invert that, is that possible?

##### Share on other sites

Very cool man! It works!One tiny question: How do I invert the table order? Right now the highest 'month' is on top, and the lowest is on the bottom. I'd like to invert that, is that possible?
That'll just be the index argument to insertRow:
`document.getElementById('myTable').insertRow(0);`

Instead of 0, use an increasing number; n-1 looks right at a quick glance:

`document.getElementById('myTable').insertRow(n-1);`

##### Share on other sites

I'm sorry to bother you again, but I can't seem to get it to work when I add my own touch to your examples. This is what I got:

`function fibonacci() {var b=document.getElementById("pairs").valuevar m=document.getElementById("months").valuevar r=document.getElementById("kids").valuevar l=document.getElementById("maxage").valuevar table=document.getElementById('myTable')var p = 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374var z = 1var y = 1var i = 0r = r/2l = l*12for (z=1; z <= l; z++){	p = p*p}for (y=1; y < m; y++){	x = x + r*i - x/p	i = x - i;	var row=document.getElementById('myTable').insertRow(y-1);	var cell0=row.insertCell(0);	var cell1=row.insertCell(1);	cell0.innerHTML=y;	cell1.innerHTML=Math.round(x);	}}`

the variables b m r l are obtained from input fields, the variable p is the number Phi. In the function you can see that im doing a few things with the final output, I'm calculating the population size once again, having a few parameters in mind [maximum age of a rabbit, number of tiny rabbits each birth].But it doesnt work! Maybe you can discover the error? I've spent pretty much time on it, but I don't seem to get it. Best,-Hugo

##### Share on other sites

... x = x + r*i - x/p...
Think you just got carried away renaming your variables: x is not defined, so this line will throw an error. ##### Share on other sites

Also, you might want to tone down the definition of p. I'm not sure what the precision for floating point numbers is in Javascript, but do an alert on p and see what the actual stored value is, see if it's any different then what you define it as.

##### Share on other sites

hey guys, read your replies. Thank you! 9 out of 10 times my the error is caused by a tiny mistake, like this undeclared variable. thank you both. I toned down the value of p as you suggested, justsomeguy. Thank you! 