Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    make sure the variable "target" is an integer.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •