Forum Moderators: open

Message Too Old, No Replies

Add an Array

         

java_junkie

12:39 am on Jun 20, 2007 (gmt 0)

10+ Year Member




<SCRIPT >

/* Program to enter the five whole numbers which are the total numbers of library books borrowed and store them in an array */

var bookArray = new Array (5);
var weekdayArray = ['Monday','Tuesday','Wednesday','Thursday','Friday']
document.write('Program that displays five whole numbers which are the total number of library books borrowed by a group of students on five successive days.');
for (var day = 0; day < bookArray.length; day = day + 1)
{
bookArray[day] = window.prompt('Enter a whole number for a day ' + (day + 1),'')
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of five whole numbers input' + '<BR>' + '<BR>');

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

</SCRIPT>

The above program prompt students to input the number of books they borrowed from a public library. How do I notify the students what days of the week they are entering value for?

java_junkie

1:12 am on Jun 20, 2007 (gmt 0)

10+ Year Member



After a lot of fiddling i've made some progress now when it prompt me to enter number of books borrowed it says every weekdays, but I need it at first prompt to say monday, and second prompt tuesday, for the third prompt wednesday till friday like that. Below is the code:

<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 + 1),'')
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of number of books borrowed on weekdays' + '<BR>' + '<BR>');

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

</SCRIPT>

rocknbil

2:00 am on Jun 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

java_junkie

2:46 am on Jun 20, 2007 (gmt 0)

10+ Year Member



Thank you! Now it's prompting for weekdays each time