Variables vvi and hhi should be with for loop turned into vv1...vv30 and hh1, hh2...hh30. Now I can't get variables tv, lv, bv and rv to return the variable(vv1 or hh1...) value instead if I put vv into quotes it turns the variable into a string (with value: vv1 or vv2...) or if vv isn't in quotes it doesn't work. I'm not good at explaining but if you know what I am talking about please help.

for(i = 1; i <= 30; i++){
	var o = $("#v"+i).css("opacity");
	if(o < 1){
	var vvi = 0;
	}else{
	var vvi = 1;
	}
	var o = $("#h"+i).css("opacity");
	if(o < 1){
		var hhi = 0;
	}else{
		var hhi = 1;
	}
	var cl = parseInt($("#"+i).attr('class'));
	var id = parseInt($("#"+i).attr('id'));
	var top = cl;
	var left = id;
	var bottom = cl+5;
	var right = id+5;
	var tv = vv+top;
	var lv = hh+left;
	var bv = vv+bottom;
	var rv = hh+right;
	var result = tv+lv+bv+rv;
	if(result==4){
		$('#'+i).html("X");
	}
	//alert(result);
}

Recommended Answers

All 6 Replies

But why variables?! Why not a #table or arrays instead?
30+ variables only for vv?!!
That's bloody expensive ...

Why don't you try arrays like:

vv[num] = value

Or even better, try explaining the goal you're after.

Sometimes it's not the code or lack of a sufficient knowledge - its the angle of approaching, and the means we choose and it happens that sometimes we chose the most difficult ones.

I tried with arrays but I get Uncaught SyntaxError: Unexpected token [(look at the comment in code)

for(i = 1; i <= 30; i++){
	var h = new Array();
	var v = new Array();
	var o = $("#v"+i).css("opacity");
	if(o < 1){
	var v[i] = 0;//on this line
	}else{
	var v[i] = 1;
	}
	var o = $("#h"+i).css("opacity");
	if(o < 1){
		var h[i] = 0;
	}else{
		var h[i] = 1;
	}
	var cl = parseInt($("#"+i).attr('class'));
	var id = parseInt($("#"+i).attr('id'));
	var top = cl;
	var left = id;
	var bottom = cl+5;
	var right = id+5;
	var tv = v[top];
	var lv = h[left];
	var bv = v[bottom];
	var rv = h[right];
	var result = tv+lv+bv+rv;
	if(result==4){
		$('#'+i).html("X");
	}
}

I tried with arrays but I get Uncaught SyntaxError: Unexpected token [(look at the comment in code)

for(i = 1; i <= 30; i++){
	var h = new Array();
	var v = new Array();
	var o = $("#v"+i).css("opacity");
	if(o < 1){
	var v[i] = 0;//on this line
	}else{
	var v[i] = 1;
	}
	var o = $("#h"+i).css("opacity");
	if(o < 1){
		var h[i] = 0;
	}else{
		var h[i] = 1;
	}
	var cl = parseInt($("#"+i).attr('class'));
	var id = parseInt($("#"+i).attr('id'));
	var top = cl;
	var left = id;
	var bottom = cl+5;
	var right = id+5;
	var tv = v[top];
	var lv = h[left];
	var bv = v[bottom];
	var rv = h[right];
	var result = tv+lv+bv+rv;
	if(result==4){
		$('#'+i).html("X");
	}
}

The thing is, - I don't understand what you are trying to do.
You should have tried to correct that error instead of escaping from water into the fire.

First thing's first: you don't declare variables inside the loop.

var h,v,o,cl,id,top,left,bottom,right,tv,lv,bv,rv,result;
for(i = 1; i <= 30; i++){
		o = $("#v"+i).css("opacity");
	if(o < 1){ v[i] = 0} else{ v[i] = 1 };
		o = $( "#h" + i ).css( "opacity" );
	if( o < 1 ){ h[i] = 0 } else{ h[i] = 1 };
		cl = parseInt( $( "#" + i ).attr( 'class' ) );
		id = parseInt( $( "#" + i ).attr( 'id' ) );
		top = cl; left = id; bottom = cl + 5; right = id + 5;
		tv = v[top]; lv = h[left]; bv = v[bottom]; rv = h[right];
		result = tv + lv + bv + rv;
	if( result == 4 ){ $( '#' + i ).html( "X" ) }
	}

Try this and tell if/what error you may encounter.

Uncaught TypeError: Cannot set property '1' of undefined

var h,v,o,cl,id,top,left,bottom,right,tv,lv,bv,rv,result;
	for(i = 1; i <= 30; i++){
		o = $("#v"+i).css("opacity");
	if(o < 1){ v[i] = 0} else{ v[i] = 1 };//this line
		o = $( "#h" + i ).css( "opacity" );
	if( o < 1 ){ h[i] = 0 } else{ h[i] = 1 };
		cl = parseInt( $( "#" + i ).attr( 'class' ) );
		id = parseInt( $( "#" + i ).attr( 'id' ) );
		top = cl; left = id; bottom = cl + 5; right = id + 5;
		tv = v[top]; lv = h[left]; bv = v[bottom]; rv = h[right];
		result = tv + lv + bv + rv;
	if( result == 4 ){ $( '#' + i ).html( "X" ) }
	}

I fixed it.
You need to declare h and v as arrays.
You asked what I am doing. It is a logic game.
I can upload it to a server if you want to see the entire code in your browser.

Yeah, that something I overlooked while trying to imose some oreder int the script

var h=[],v=[],o,cl,id,top,left,bottom,right,tv,lv,bv,rv,result;

This is how it should have looked in e completed code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.