Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For loop not detecting 1

Status
Not open for further replies.

cellpeter

Programmer
Dec 30, 2004
34
US
Hello,

I am trying to create a function where data in a table row if duplicated e.g. 4, then the function will display an alert indicating that the function has detected a row of data which is exactly the same

So far for any experiment I've used the function to detect only the string '4' in a row...

However for some reason the function isn't working, it doesn't detect the correct data in a row

----------


Here is the code so far


-------
Code:
<script language="javascript">



function test()
{
VV = document.getElementsByTagName('td');

for(A = 0;A < VV.length;A++)
{

VV[A].innerHTML = '45'
VV[A].setAttribute('onClick',"this.innerHTML = '4'")

}





}

function grr()
{
Y = new Array();
TY = 0;
XX = document.getElementsByTagName('td');
NN = document.getElementsByTagName('td');
for(A = 0;A < XX.length;A++)
{
	Y[A] = XX[A].innerHTML;	
	}
alert(Y.length)
for(BB = 8 * TY;BB < Y.length;BB = BB + 8)
{
alert(TY)	

//Problem Area
if('4'==Y[BB].innerHTML && '4'==Y[BB+1].innerHTML && '4'==Y[BB+2].innerHTML  && '4'==Y[BB+3].innerHTML && '4'==Y[BB+4].innerHTML && '4'==Y[BB+5].innerHTML && '4'==Y[BB+6].innerHTML  && '4'==Y[BB+7].innerHTML)
{
	
	alert('a row of fours have been detected');
}	
TY++	
}
}
window.onload = test;
</script>
<table cellspacing=0 width=670 border=1>
<td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><tr>
<td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><tr>
<td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><tr>
<td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><tr>
</table>
<input type="button" onClick=grr()>
----------------

I checked the javascript console and it has not displayed any errors..

Any help would be very appreciated


programmers make extra money
>>
 
The DOM for tables and textNodes is very different from IE to Firefox or the like. Are you programming for one particular browser? Since you say "javascript console" I think that you might be using Firefox in which case you need to look at textNodes...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Code:
function grr(){
	Y = new Array();
	TY = 0;
	XX = document.getElementsByTagName('td');
	NN = document.getElementsByTagName('td');
	for(A = 0;A < XX.length;A++){
	    Y[A] = XX[A].innerHTML;    
	}
	alert(Y.length)
	for(BB = 8 * TY;BB < Y.length;BB = BB + 8){
		alert(BB)    
		//alert(Y)
		//Problem Area
		if('4'==Y[BB] && '4'==Y[BB+1] && '4'==Y[BB+2]  && '4'==Y[BB+3] && '4'==Y[BB+4] && '4'==Y[BB+5] && '4'==Y[BB+6]  && '4'==Y[BB+7])
		{
		    alert('a row of fours have been detected');
		}    
		TY++    
	}
}

I removed the ".innerHTML" from the if statements since you are comparing the values to an array. It worked in Firefox when all 8 values in the row = '4'

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top