PDA

View Full Version : Array help



DerekAcorah
06-12-2007, 05:24 PM
Hi people!

Im new to Javascript and need a bit of help. OK, ive got a program, but when it prompts me to enter the 5 numbers, it says every day of the week, i now need it so that on the first prompt its monday, 2nd prompt tuesday etc etc,

Can anyone point me in the right direction (i don't want the answer, i just need some direction because im clueless!) Please help!


<HTML>
<HEAD>
<TITLE> Number of library books borrowed
</TITLE>
<SCRIPT >

/* Program to read in a known number of data items and store them in an array */

var booksArray = new Array (5);
var dayNamesArray = ['Monday','Tuesday','Wednesday','Thursday','Friday'];

document.write('Array program to read in a known number of data items');
for (var day = 0; day < booksArray.length; day = day + 1)
{
booksArray[day] = window.prompt('Enter number of books borrowed on ' + (dayNamesArray + 1),'')
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of number of books borrowed' + '<BR>' + '<BR>');

for (var day = 0; day < booksArray.length; day = day + 1)
{
document.write(dayNamesArray[day] + ' : ' + booksArray[day] + '<BR>')
}


</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

boogyman
06-12-2007, 08:08 PM
after you have created the "days" you need to obtain the date from the server. That will return a number 0-6, which happens to coincide with the 7 days of the week. you will need to modify your script though, to not include 0 and 6 since those represent sat/sun.

DerekAcorah
06-12-2007, 08:13 PM
Thank you for your help boogyman.

I only need the days Monday, Tuesday, Wednesday, Thursday, and Friday, and also doesn't the 0 therefore represent wich day you have stated first in the array?

boogyman
06-12-2007, 08:18 PM
yes in your array, but if you are attempting to coincide with an ACTUAL day, you will need to retrieve the date of the server, which will return 0-6 (sun-sat)

if you are doing it solely for your own purposes then yes, you do not need to worry about that

Twey
06-12-2007, 10:26 PM
I'm guessing this is a homework problem? If so, there's no need to worry about communicating with the server (if it's a serious project, then as boogyman says, you need to get the date down from the server somehow any not rely on the client's clock). You also want to validate your page (as HTML 4.01 Strict) and avoid using document.write(). You want:
(dayNamesArray[i])not
(dayNamesArray + 1)too.

DerekAcorah
06-13-2007, 06:22 AM
Yeah it is a coursework problem, so based on that, do i still need to change the dayNamesArray bit?

Twey
06-13-2007, 01:15 PM
Yes. I strongly suggest you take my other suggestions too.

An element of a Javascript array is accessed using the syntax:
myArray[index];

boogyman
06-13-2007, 01:42 PM
avoid using document.write()
what as that been replaced with?

Twey
06-13-2007, 02:01 PM
A new node should be appended to the document containing the desired text.
document.body.appendChild(
document.createElement("p")
).appendChild(
document.createTextNode("Lion lion lion");
);