Jump to content

[SOLVED] accessing array elements


ninedoors

Recommended Posts

I have a function that takes an input and writes that input to an html table cell.  I have 18 cells that the user inputs a number to.  What I want to is add the numbers the user inputs up  and write it to the final cell as a total.  z, y have been declared outside the function so they are global.  Here's the function:

 

function s (t){

var holes = [ "hole21","hole22","hole23","hole24","hole25","hole26","hole27","hole28","hole29","hole31","hole32","hole33","hole34","hole35","hole36","hole37","hole38","hole39" ];
var r = holes[z];
document.getElementById(r).innerHTML=t.innerHTML;
z++;
y += t.innerHTML;

if(r == 'hole29')	{
					document.getElementById('hole30').innerHTML=y;
					}
}

 

Where t is the user input(an integer), holes is the array of table cell ids where the user input values are being shown

 

My first thought was to just have a variable that would take would be say var y and then have something like y =y+t;

 

And have a running total, then when I reached the given cell that I wanted to put the total in (holes30) I could just write y to that cell.  Does that sound right?

 

I tried do that but I could get it to work any suggestions?

Link to comment
Share on other sites

Here's the working code in case any one is interested

 

function s (t){

var holes = [ "hole21","hole22","hole23","hole24","hole25","hole26","hole27","hole28","hole29","hole31","hole32","hole33","hole34","hole35","hole36","hole37","hole38","hole39" ];
var r = holes[z];
document.getElementById(r).innerHTML=t.innerHTML;
var scores = new Array();

z++;

if(r == 'hole29')	{
					ftotal = 0;
					for(var i=0;i<9;i++)	
					{
					var u = holes[i];
					var p = document.getElementById(u).innerHTML;
					ftotal+=Number(p);
					}
					document.getElementById("hole30").innerHTML=ftotal;
					}

if(r == 'hole39')	{
					btotal = 0;
					total = 0;
					for(var i=9;i<18;i++)	
					{
					var u = holes[i];
					var p = document.getElementById(u).innerHTML;
					btotal+=Number(p);
					}
					document.getElementById("hole40").innerHTML=btotal;
					total = ftotal + btotal;
					document.getElementById("hole41").innerHTML=total;
					}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.