Web Analytics Made Easy -
StatCounter for loop and manual coding - CodingForum

Announcement

Collapse
No announcement yet.

for loop and manual coding

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • for loop and manual coding

    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

  • #2
    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.
    As easy as 3.1415926535897932384626433832795028841

    Comment


    • #3
      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
      As easy as 3.1415926535897932384626433832795028841

      Comment


      • #4
        make sure the variable "target" is an integer.

        Comment

        Working...
        X