Jump to content

Write Output to tables


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 x[0]Month m+1 Population size x[1]Month m+2 Population size x[2]Here'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

Link to post
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 x[0]Month m+1 Population size x[1]Month m+2 Population size x[2]Here'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>

Link to post
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);

Link to post
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

Link to post
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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...