...

View Full Version : Please Help Cycling Through Variables!



Scriptet
04-21-2008, 10:49 PM
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.


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!

mjlorbet
04-21-2008, 11:16 PM
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
}
}

Scriptet
04-22-2008, 02:33 PM
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:


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.)

mjlorbet
04-22-2008, 03:02 PM
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>");
}

Scriptet
04-22-2008, 04:25 PM
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:

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
(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.

mjlorbet
04-22-2008, 04:27 PM
you need to define game so you can use it outside of the scope of the loop, so put
var game=""; above for(...)

Scriptet
04-22-2008, 04:33 PM
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:
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.

mjlorbet
04-22-2008, 04:41 PM
oh, lol, terribly sorry, it should be tmp-1 instead of tmp being passed to the array

Scriptet
04-22-2008, 05:13 PM
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:confused:
Thanks again it's really appreciated!

mjlorbet
04-22-2008, 05:27 PM
game = gmarr[tmp];
becomes

game = gmarr[tmp-1];

Scriptet
04-22-2008, 05:44 PM
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?

mjlorbet
04-22-2008, 06:09 PM
if((getElement(tmp)>=today) && !already_found){


sorry, again, i forgot to add it ;)

Scriptet
04-22-2008, 07:44 PM
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!

mjlorbet
04-22-2008, 07:56 PM
today = window[create_Is(++tmp)];

or

today = window[fname + "i"]

or

today = window[create_Is(fname.length+1)]

Scriptet
04-22-2008, 10:35 PM
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?

mjlorbet
04-22-2008, 10:43 PM
you would have to save the value of tmp at the time the game was selected.



var foundIndex -1;
for(...){
if(getElement(tmp)>=today){
// .....
foundIndex = tmp;
}
}



then operate on foundIndex instead of tmp for your nxt() function

Scriptet
04-23-2008, 02:03 AM
Hey thanks for the help, i've now got it working succesfully with prev and next buttons! Thanks for your help all the way through it's really appreciated, this is my first JS ever, although you created pratically all of it I am still happy :)

For education purposes could you explain to me what these functions actually do:

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)];
}

mjlorbet
04-23-2008, 02:08 AM
create_Is is actually pretty cheap, it just creates strings with different numbers of the letter i
e.g.


create_Is(1) returns i
create_Is(2) returns ii
create_Is(3) returns iii
etc.
getElement is a little more complicated, it retrieves variables by their name from window scope

so you could do
var iiiiiiiiiiiiiiiiii = "happy";
and the breakdown of the getElement call to get it would be:


getElement(18) -> return window[create_Is(18)] -> return window["iiiiiiiiiiiiiiiiii"] -> return "happy"

as window["something"] maps to the declaration of var something outside of any function (window scope)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum