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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts

    Thumbs up Please Help Cycling Through Variables!

    Hi All, I have just joined the forums, after starting to learn JavaScript off my own back. Basically I have a simple problem and would be really greatful if somebody could help me out please!

    Below you can see my code, what I do is define new dates and then check them against todays date later on in an if statement. However i need it so that i can increment via i++ to move onto the next function, and do not know how to do this.

    Code:
    var i=new Date();
    i.setFullYear(2008,03,22);
    i.setHours(19,45)
    
    var ii=new Date();
    ii.setFullYear(2008,03,26);
    ii.setHours(15,00)
    
    var iii=new Date();
    iii.setFullYear(2008,03,30);
    iii.setHours(19,45)
    
    var today = new Date();
    
    if (i>=today)
    {
    blahblah
    }
    else if (ii>=today)
    {
    blahblah
    }
    else if (iii>=today)
    {
    blahblah
    }
    else
    etc......This works as it is, but I need is so that I can use i++ to increment from one variable onto the next defined variable. For example Say i do this:
    something = var i
    I then want a next button which allows i++, which will then make
    something = var ii

    I know how to set up the buttons and everything I just need to know how to put it inside an array (if this is what is needed) Could somebody please help me out!

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    function create_Is(n){
    var tmp = "";
    for(var t = 0; t < n; t++)
        tmp += "i";
    return tmp;
    }
     
    function getElement(index){
    return window[create_Is(index)];
    }
     
    for(var tmp = 1; tmp <= number_of_dates; tmp++){
        if(getElement(tmp)>=today){
            blahblah
        }
    }
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    Scriptet (04-22-2008)

  • #3
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hi, Thanks for the reply! I've been trying to work with your given code, but i really am a noob, therefore i'm unsure on how to implement it into my current code. OK below is my current code in full:

    Code:
    var i=new Date();
    i.setFullYear(2008,03,22);
    i.setHours(19,45)
    
    var ii=new Date();
    ii.setFullYear(2008,03,26);
    ii.setHours(15,00)
    
    var iii=new Date();
    iii.setFullYear(2008,03,30);
    iii.setHours(19,45)
    
    var today = new Date();
    
    if (i>=today)
    {
    fname = i;
    game = "Chelsea"; 
    stadium = "Anfield";
    comp = "CL Semi-Final 1st Leg";
    image_to_show = "chelsea.jpg"; 
    }
    else if (ii>=today)
    {
    fname = ii;
    game = "Birmingham";
    stadium = "St Andrews"; 
    comp = "English Premier League";
    image_to_show = "birmingham.jpg"; 
    }
    else if (iii>=today)
    {
    fname = iii;
    game = "Chelsea Leg 2"; 
    stadium = "Stamford Bridge";
    comp = "CL Semi-Final 2nd Leg";
    image_to_show = "chelsea.jpg"; 
    }
    else
    {
    alert("ththtrhtrh");
    }
    document.write ("<img id=\"e\" src=\"" + image_to_show + "\"///></div></div><p>" + "Next Fixture Vs <span id=\"c\">" + game + "</span> at <span id=\"d\">" + stadium + "</span><br/>" + "<span id=\"b\">" + comp + "</span></p>");
    }
    Description on what it does: OK this is basically my first JS so i've probably gone the wrong way about it. Basically it checks for the next fixture compared to the date, this works fine. This is not the problem.

    What I need is a next match function, which will move it onto the next variable. So say the result from the earlier if statement is outputting the details of var ii, i then need to move it onto var iii when the next match button is clicked. So Basically I need to be able to define:

    today = n++ (where n++ changes todays date to that of the next defined variable set in the list at the top of the code shown.)

  • #4
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    var i=new Date();
    i.setFullYear(2008,03,22);
    i.setHours(19,45);
    
    var ii=new Date();
    ii.setFullYear(2008,03,26);
    ii.setHours(15,00);
    
    var iii=new Date();
    iii.setFullYear(2008,03,30);
    iii.setHours(19,45);
    
    var today = new Date();
    
    function create_Is(n){
    var tmp = "";
    for(var t = 0; t < n; t++)
        tmp += "i";
    return tmp;
    }
    
    function getElement(index){
    return window[create_Is(index)];
    }
    
    var gmarr = ["Chelsea", "Birmingham", "Chelsea Leg 2"];
    var stadiumarr = ["Anfield", "St Andrews", "Stamford Bridge"];
    var comparr = ["CL Semi-Final 1st Leg", "English Premier League", "CL Semi-Final 2nd Leg"];
    var image_to_showarr = ["chelsea.jpg", "birmingham.jpg", "chelsea.jpg"];
     
    var already_found = false;
    for(var tmp = 1; tmp <= 3; tmp++){
        if(getElement(tmp)>=today){
            already_found = true;
            fname = create_Is(tmp);
            game = gmarr[tmp];
            stadium = gmarr[tmp];
            comp = comparr[tmp];
            image_to_show = image_to_showarr[tmp];
        }
    }
    if(!already_found)
        alert("ththtrhtrh");
    document.write ("<img id=\"e\" src=\"" + image_to_show + "\"///></div></div><p>" + "Next Fixture Vs <span id=\"c\">" + game + "</span> at <span id=\"d\">" + stadium + "</span><br/>" + "<span id=\"b\">" + comp + "</span></p>");
    }
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    Scriptet (04-22-2008)

  • #5
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hey thanks again, well on trying the code I could not get it to output a correct. For example here is your code in minimized form, so that I could test it:
    Code:
    var i=new Date();
    i.setFullYear(2008,03,23);
    i.setHours(19,45);
    
    var ii=new Date();
    ii.setFullYear(2008,03,26);
    ii.setHours(15,00);
    
    var today = new Date();
    
    function create_Is(n){
    var tmp = "";
    for(var t = 0; t < n; t++)
        tmp += "i";
    return tmp;
    }
    
    function getElement(index){
    return window[create_Is(index)];
    }
    
    var gmarr = ["Chelsea", "Birmingham"];
     
    var already_found = false;
    for(var tmp = 1; tmp <= 2; tmp++){
        if(getElement(tmp)>=today){
            already_found = true;
            game = gmarr[tmp];
        }
    }
    document.write ("test" + game );
    This should output testChelsea, but instead it outputs testundefined. It seems to want to output the third value in the gmarr, which does not exisit as I have only defined 2. But it should be ouputting the first value in the array, as the date of this
    Code:
    (i.setFullYear(2008,03,23);)
    is > today's date.

    I need the loop to go through the variables i, ii, iii, etc.. in sequence and the first one of these that => today, it takes there gmarr value, and stops there.
    Last edited by Scriptet; 04-22-2008 at 05:27 PM.

  • #6
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    you need to define game so you can use it outside of the scope of the loop, so put
    Code:
    var game="";
    above for(...)
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #7
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hey thanks for the quick reply again.
    I tried this but it still outputs undefined.
    If I define a third value inside the gmarr it outputs this instead for example:
    Code:
    var gmarr = ["Chelsea", "Birmingham", "Testing"];
    it would output this third value, when it should be outputting the first value, because this (var i) is the first case where it is > today.

  • #8
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    oh, lol, terribly sorry, it should be tmp-1 instead of tmp being passed to the array
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #9
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    It's OK, i'm sorry for being a complete noob, but how do i change it from tmp to tmp-1 being passed onto the arrray
    Thanks again it's really appreciated!

  • #10
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    game = gmarr[tmp];
    becomes

    game = gmarr[tmp-1];

    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    Scriptet (04-22-2008)

  • #11
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hey OK, now it is outputting a result. There is just one last problem.
    We have the variables defined var i var ii var ii. var ii...var x etc. at the top of the JS.

    The problem is it is outputting the LAST true result, i.e the last variable that is >=today, when it should be outputting the FIRST true result i.e the first variable that is >=today.

    So if var i var ii and var iii are ALL true, and >=today, it should be outputting var i's gmarr, not var iii's gmarr.

    Maybe we need the for loop to stop once there is a true result?

  • #12
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts

    if((getElement(tmp)>=today) && !already_found){


    sorry, again, i forgot to add it
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    Scriptet (04-22-2008)

  • #13
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hey nice one! It's outputting the correct data now!!
    OK now the very last thing i need to do:

    Once the if statement is done it chooses one of my variables say var ii.
    Now what i need to do is after this is move onto the next variable in the list and make today equal the date of that variable e.g:
    today = varii + 1
    so today = vari iii in this instance,

    so that today will equal the date of the next variable in the list.
    I tried:

    today = tmp++ , but made today equal the number 4 rather than date!

  • #14
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    today = window[create_Is(++tmp)];

    or

    today = window[fname + "i"]

    or

    today = window[create_Is(fname.length+1)]
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #15
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Hi, Many Thanks again! This works great: today = window[fname + "i"], never thought about it like that.

    What I was initally going to do for a next button was to set todays date to the next fixture and then loop through the for statement again.

    However on thinking now that it is set up into an array couldnt we just skip onto the next values for the array.

    For example:

    function nxt()
    {
    game = gmarr[tmp++];
    }

    The problem I am having is that tmp++ doesn't skip it onto the next value in the array, I am obviously using the wrong command here but cannot figure out what it should be?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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