/    Sign up×
Community /Pin to ProfileBookmark

passing a variable to an array name

hey all,
I’m trying (badly) to create a variable and pass it to a document.write so i calls the array name. I have my code to get what the number of the week is for the year and from that i have to use it to determine whether team a or team b are on shift. I have multiple arrays A1, A2, A3 and B1, B2, B3 what i need is once my script determines what week it is, it divides it by 2 and then if the remainder is 0 team A are on or if 1 team B are on.
I then set a variable of weekteam which i then try to use to reference an array

if (weeknum % 2 == 0)
var weekteam=”A”;
else
var weekteam=”B”;

var weekname1=(weekteam + “1”);

document.write(‘<BR> test ‘ + ‘$weekname1′[5]);

Its probably something simple here but for the life of me i cant get it. Any help would be appreciated.
Thanks
Paddy

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 25.2007 — Couple of things I don't understand in your code:

[code=php]
if (weeknum % 2 == 0)
var weekteam="A";
else
var weekteam="B";

var weekname1=(weekteam + "1");

document.write('<BR> test ' + '$weekname1'[5]);
[/code]


Where is the "1" comming from in 'var weekname1=(weekteam+"1")'?

I thought the week number was supposed to change.

I assume you are using some sort of database that requires

the '$weekname1'[5] nomenclature, but for the document.write

command I would possibly use $weekname[5]. Without the (') chars

Could it look like this?
[code=php]
var weekteam = 'B';
if (weeknum % 2 == 0) {weekteam="A"; }

var weekname1=(weekteam + "1"); // "1" ... a constant or a variable?

document.write('<BR> test ' + $weekname1[5]); // '$' used how ... some DB?
[/code]

Or possibly:
[code=php]
var weekteam1 = 'B1';
if (weeknum % 2 == 0) {weekteam1="A1"; }
document.write('<BR> test ' + weekname1[5]);
[/code]

Note: Your code only references the [5] element of weekname1

but I don't see where this is ever defined.

Final note: What kind of errors are you getting in the error console?
Copy linkTweet thisAlerts:
@paddygmanauthorNov 25.2007 — I'll try to explain further, (I'm not very good with Javascript as you'll be able to tell) I'm trying to publish a page which displays whether 2 groups are on certain hours each day and which group is on. The rota involves Group A being on one week and group B being the next. I've managed to get code for checking the week and using it set a variable which intended to use to check an array which has the values for each member of the teams. Team A will have 8 arrays A1 to A8 and likewise for team B

I wish to use the variable created to call the value from the array at

weekteam1,2,3...8(weeknumber) or A1(47) or A2(47)

I'm just having difficulty passing the weekteam to the name of the array, this is why i have the variable weekname1 as i created a new variable

weekname1=(weekname + "1")

to test.

Here is all of my code including test lines, be warned it is messy.


[code=php]<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function showWeek() {
var today = new Date();
var year = y2k(today.getYear());
var month = today.getMonth();
var day = today.getDate();
return getWeek(year,month,day);
}

function getWeek(year,month,day) {
var newYear = new Date(year,0,1);
var offset = 7 + 1 - newYear.getDay();
if (offset == 8) offset = 1;
var daynum = ((Date.UTC(y2k(year),month,day,0,0,0) -
Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
var weeknum = Math.floor((daynum-offset+7)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevOffset = 7 + 1 - prevNewYear.getDay();
if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
}
return weeknum;
}


<!-- Today is Stuff -->
var weeknum = showWeek()
var d = new Date()
theDay=d.getDay()
var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"

document.write('This week is week number ' + weeknum );
document.write('<br>Today is ' + weekday[d.getDay()]);





var A1=['LC','EC','CC','LC','EC','CC'];
var A2=['LC','EC','CC','LC','EC','CC'];
var A3=['LC','EC','CC','LC','EC','CC'];
var A4=['LC','EC','CC','LC','EC','CC'];
var A5=['LC','EC','CC','LC','EC','CC'];
var A6=['LC','EC','CC','LC','EC','CC'];
var A7=['LC','EC','CC','LC','EC','CC'];
var A8=['LC','EC','CC','LC','EC','CC'];
var B1=['LC','EC','CC','LC','EC','CC'];
var B2=['LC','EC','CC','LC','EC','CC'];
var B3=['LC','EC','CC','LC','EC','CC'];
var B4=['LC','EC','CC','LC','EC','CC'];
var B5=['LC','EC','CC','LC','EC','CC'];
var B6=['LC','EC','CC','LC','EC','CC'];
var B7=['LC','EC','CC','LC','EC','CC'];
var B8=['LC','EC','CC','LC','EC','CC'];

var earlycatcher=['EC','EC','EC'];
var latecatcher=['LC','LC','LC'];
var fridaylatecatcher=['FLC','FLC','FLC'];
var changecatcher=['CC','CC','CC'];
var floatingcatcher=['FC','FC','FC'];
var weeknumfake=5;


if (weeknum % 2 == 0)
var weekteam="A";
else
var weekteam="B";


var weekname1=(weekteam + "1");

document.write('<BR> This Week Catcher Team ' + weekteam + ' are on Duty')
document.write('<br> weekname1 is ' + weekname1);
document.write('<BR> test ' + '$weekname1'[5]);



//-->
</SCRIPT>[/code]
Copy linkTweet thisAlerts:
@JMRKERNov 25.2007 — I'm trying to use your existing code,

but I'm still missing the need for certain code you put in (see NOTE: in script)

[code=php]
<html>
<head>
<title>Array Rotation</title>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">

// function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function showWeek() {
var today = new Date();
// var year = y2k(today.getYear());
var year = today.getFullYear();
var month = today.getMonth();
var day = today.getDate();
return getWeek(year,month,day);
}

function getWeek(year,month,day) {
var newYear = new Date(year,0,1);
var offset = 7 + 1 - newYear.getDay();
if (offset == 8) offset = 1;
// var daynum = ((Date.UTC(y2k(year),month,day,0,0,0) -
var daynum = ((Date.UTC(year,month,day,0,0,0) -
// Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
Date.UTC(year,0,1,0,0,0)) /1000/60/60/24) + 1;
var weeknum = Math.floor((daynum-offset+7)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevOffset = 7 + 1 - prevNewYear.getDay();
if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
}
return weeknum;
}

function Init() {
var weeknum = showWeek()
var d = new Date()
theDay=d.getDay()
var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"

// NOTE: Need to define Array(s) correctly
var A1= new Array('LC','EC','CC','LC','EC','CC');
var A2= new Array('LC','EC','CC','LC','EC','CC');
var A3= new Array('LC','EC','CC','LC','EC','CC');
var A4= new Array('LC','EC','CC','LC','EC','CC');
var A5= new Array('LC','EC','CC','LC','EC','CC');
var A6= new Array('LC','EC','CC','LC','EC','CC');
var A7= new Array('LC','EC','CC','LC','EC','CC');
var A8= new Array('LC','EC','CC','LC','EC','CC');
var B1= new Array('LC','EC','CC','LC','EC','CC');
var B2= new Array('LC','EC','CC','LC','EC','CC');
var B3= new Array('LC','EC','CC','LC','EC','CC');
var B4= new Array('LC','EC','CC','LC','EC','CC');
var B5= new Array('LC','EC','CC','LC','EC','CC');
var B6= new Array('LC','EC','CC','LC','EC','CC');
var B7= new Array('LC','EC','CC','LC','EC','CC');
var B8= new Array('LC','EC','CC','LC','EC','CC');

// NOTE: If these are Array definitions, they need to be corrected also
var earlycatcher=['EC','EC','EC'];
var latecatcher=['LC','LC','LC'];
var fridaylatecatcher=['FLC','FLC','FLC'];
var changecatcher=['CC','CC','CC'];
var floatingcatcher=['FC','FC','FC'];
var weeknumfake=5;

var weekteam = 'B1';
if (weeknum % 2 == 0) {weekteam="A1"; }
// NOTE: do not see need to add '1' to weekname1 below
// var weekname1=(weekteam+'1');
var weekname1=weekteam;

// NOTE: cannot find this array anywhere
// document.write('<BR> test ' + weekname1[5]);

document.write('This week is week number ' + weeknum );
document.write('<br>Today is ' + weekday[d.getDay()]);

document.write('<BR> This Week Catcher Team ' + weekteam + ' are on Duty')
document.write('<br> weekname1 is ' + weekname1);
document.write('<BR> test ' + weekname1);

document.write('<br />Contents of: '+weekname1+'<br />');
if (weekname1 == 'A1') { for (var i=0; i<A1.length; i++) { document.write(A1[i]+','); } }
if (weekname1 == 'A2') { for (var i=0; i<A2.length; i++) { document.write(A2[i]+','); } }
if (weekname1 == 'A3') { for (var i=0; i<A3.length; i++) { document.write(A3[i]+','); } }
if (weekname1 == 'A4') { for (var i=0; i<A4.length; i++) { document.write(A4[i]+','); } }
if (weekname1 == 'A5') { for (var i=0; i<A5.length; i++) { document.write(A5[i]+','); } }
if (weekname1 == 'A6') { for (var i=0; i<A6.length; i++) { document.write(A6[i]+','); } }
if (weekname1 == 'A7') { for (var i=0; i<A7.length; i++) { document.write(A7[i]+','); } }
if (weekname1 == 'A8') { for (var i=0; i<A8.length; i++) { document.write(A8[i]+','); } }
if (weekname1 == 'B1') { for (var i=0; i<B1.length; i++) { document.write(B1[i]+','); } }
if (weekname1 == 'B2') { for (var i=0; i<B2.length; i++) { document.write(B2[i]+','); } }
if (weekname1 == 'B3') { for (var i=0; i<B3.length; i++) { document.write(B3[i]+','); } }
if (weekname1 == 'B4') { for (var i=0; i<B4.length; i++) { document.write(B4[i]+','); } }
if (weekname1 == 'B5') { for (var i=0; i<B5.length; i++) { document.write(B5[i]+','); } }
if (weekname1 == 'B6') { for (var i=0; i<B6.length; i++) { document.write(B6[i]+','); } }
if (weekname1 == 'B7') { for (var i=0; i<B7.length; i++) { document.write(B7[i]+','); } }
if (weekname1 == 'B8') { for (var i=0; i<B8.length; i++) { document.write(B8[i]+','); } }
}
</SCRIPT>
</head>
<body onLoad="Init()">

</body>
</html>
[/code]

Note also that there is not a need for the y2k function using the getFullYear()

Modify to your hearts' content.

Good Luck!
Copy linkTweet thisAlerts:
@paddygmanauthorNov 25.2007 — Thanks for the help.

I can see what you meant with the variable and the 1 on the end now and i think i may be able to edit your code do do what i need although still not answering the method i had originally intended but probably for the best. I'll post back if i get stuck again.

Thanks again.
×

Success!

Help @paddygman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...