...

View Full Version : Array help



DerekAcorah
06-10-2007, 05:33 PM
Hello everyone,
I am having a problem implamenting an array code into a program. I have a program that lets you input 5 things, but for each time a user inputs information, i want it to tell me the day of the week, from Monday - Friday. I have got the code so it it does it on a day 1 to day 5 basis, and ive made an attempt at the Monday to Friday bit, but i can't get it going. What i have done so far is below, can anyone tell me where i have gone wrong ( I don't want the answer because id like to work it out for myself, but some hints as to what i have got to do would be nice!)

Thanks :)

<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)
{
dayNamesArray[day] = window.prompt('Enter number of books borrowed on ' + (day + 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] + '<BR>')
}

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

Philip M
06-10-2007, 07:16 PM
Hint: JavaScript is case sensitive. Check your spelling of Day/day.

No semi-colon after }

DerekAcorah
06-10-2007, 07:39 PM
Thanks for your help, i made the amendments you suggested, but i am still getting day 1 day 2 etc etc

DerekAcorah
06-10-2007, 11:00 PM
OK, after lots of fiddling, ive made some progress, now 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 (again i don't want the answer, i just need some direction because im clueless!) Please help!

(Here is my revised version)

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

webby07
06-20-2007, 05:21 AM
<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('Program that list total number of books borrowed from a library');
for (var day = 0; day < booksArray.length; day = day + 1)
{
booksArray[day] = window.prompt('Enter number of books borrowed on ' + (dayNamesArray[day]),'');
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of number of books borrowed on weekdays' + '<BR>' + '<BR>');

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

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

You almost had it! :-) Look at your last 'for' loop. It's the same thing.

booksArray[day] = window.prompt('Enter number of books borrowed on ' + (dayNamesArray[day]),'');

Also it won't change the function any, but you can use ++ to increment a for loop.

for (day=0;day<booksArray.length;day++)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum