...

View Full Version : for loop and manual coding



volkany
06-27-2002, 01:21 PM
hi all, i have two javascript functions like that:


function changeCont_1(target) {
for (var i = 0; i <= 3; i++) {
var pgl = 'p' + i + 'l', pgr = 'p' + i + 'r';
if (i == target) {
getObj(pgl).style.visibility = "visible";
getObj(pgr).style.visibility = "visible";
} else {
getObj(pgl).style.visibility = "hidden";
getObj(pgr).style.visibility = "hidden";
}
}
}

function changeCont_2(target) {
if (target == 1) {
getObj("p1l").style.visibility = "visible";
getObj("p1r").style.visibility = "visible";
getObj("p2l").style.visibility = "hidden";
getObj("p2r").style.visibility = "hidden";
getObj("p3l").style.visibility = "hidden";
getObj("p3r").style.visibility = "hidden";
} else if (target == 2) {
getObj("p2l").style.visibility = "visible";
getObj("p2r").style.visibility = "visible";
getObj("p1l").style.visibility = "hidden";
getObj("p1r").style.visibility = "hidden";
getObj("p3l").style.visibility = "hidden";
getObj("p3r").style.visibility = "hidden";
} else if (target == 3) {
getObj("p3l").style.visibility = "visible";
getObj("p3r").style.visibility = "visible";
getObj("p1l").style.visibility = "hidden";
getObj("p1r").style.visibility = "hidden";
getObj("p2l").style.visibility = "hidden";
getObj("p2r").style.visibility = "hidden";
}
}


changeCont_2 is working perfectly, but as u could see it's not very programmer friednly. so i tried to make a new version of it in changeCont_1, but this time changeCont_1 isn't working. could soomebody help me about this problem...

best regards

head8k
06-27-2002, 01:28 PM
Hi

I haven't got time to have a proper look at this but my first thought is that this line:

for (var i = 0; i <= 3; i++)

could be where the error lies. You are asking the loop to increase the counter i until it is less than or equal to 3. Which do you want? Remove the equals sign so it's:

for (var i = 0; i < 3; i++)

and try again.

head8k
06-27-2002, 01:51 PM
Also spotted another mistake in this line:

var pgl = 'p' + i + 'l', pgr = 'p' + i + 'r';

You should use ; if you are separating statements not ,

Try:

var pgl = 'p' + i + 'l';
var pgr = 'p' + i + 'r';

instead

RadarBob
06-27-2002, 02:06 PM
make sure the variable "target" is an integer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum