...

View Full Version : Array's



neilbigh101
06-14-2007, 10:20 AM
Good morning to all from a rainy Manchester,

as i`ve said before new to javaScript, (and pc's come to that) treid writing my first Array but cant` get it to run (though i am pleased i had a go) could anybody have a look at it and help me out many, many thanks Neil

<html>
<head>
<title>Array's
</title>
<script>

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

var bookArray = new Array (5)

document.write(' Array to read in a nuber of data items');
for (var day = 0; day <bookArray.length; day = day + 1)
{
bookArray[day] = window.promt(' enter books value for day ' + (day + ),'')
};
document.write('<BR>' + '<BR>');
document.write('confirmation of data input' + '<BR>' + '<BR>');

for (var day = 0; day < bookArray.lenght; day = day + 1)

{
document.write(bookArray[day] + '<BR>')
}

</script>
</head>
<body>
</body>
</html>

Philip M
06-14-2007, 10:48 AM
As I mentioned before, accuracy is vital.
Check your spelling of prompt and length.
Misplaced brackets will also cause the script to fail.

It would also be a good idea to check that the values entered by the user are numbers.

<html>
<head>
<title>Arrays</title>
<script type = "text/javascript">
/* Program to read in a known number of data and store them in an array */

var bookArray = new Array (5);

document.write(' Array to read in a number of data items');
for (var day = 0; day <bookArray.length; day = day + 1) {
bookArray[day] = window.prompt(' enter books value for day ' + day,'')
}
document.write('<BR>' + '<BR>');
document.write('confirmation of data input' + '<BR>' + '<BR>');

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

</script>
</head>
<body>
</body>
</html>

neilbigh101
06-14-2007, 11:01 AM
Morning Philip, you my friend are an absolute genius, have a good day

neilbigh101
06-14-2007, 06:52 PM
Evening Philip, i know im mithering you to death, but how would i alter it to show the day which saw the most books go out, seems there is a couple of pages missing from that sylabus i told you about it asks the question and it just jumps to intrest rates tried allday, havent got any idea at all, playing about with that other code you posted has been a revelation, keep toying with it and seems to be sinking in, finally, once again thanking you in anticipation best regards Neil

Philip M
06-14-2007, 07:53 PM
<html>
<head>
<title>Arrays</title>
<script type = "text/javascript">
/* Program to read in a known number of data and store them in an array */

var bookArray = new Array (5);
var highestNum = 0;
var highestDay = 0;
document.write(' Array to read in a number of data items');

for (var day = 0; day <bookArray.length; day = day + 1) {
bookArray[day] = window.prompt('Enter books value for day ' + day,'')
// Update highestNum and highestDay if highestNum is greater than previous value
if (bookArray[day] > highestNum) {
highestNum = parseInt(bookArray[day]); // number not string
highestDay = day;
}
}
document.write('<BR>' + '<BR>');
document.write('confirmation of data input' + '<BR>' + '<BR>');

for (var day = 0; day < bookArray.length; day = day + 1) {
document.write(bookArray[day] + '<BR>');
}
document.write("<BR>" + "The highest number of books was " + highestNum + " on day " + highestDay);

</script>
</head>
<body>
</body>
</html>

Note that the array index starts at day 0, so the five days are 0,1,2,3,4.

You can change this to make the days 1-5 by making var bookArray = new Array (6);
and
for (var day = 1 ...........

Suggestion: revise your script to show the days as Monday, Tuesday etc.

Let me have your mailing address, please, so that I can send my bill! ;)

neilbigh101
06-14-2007, 09:13 PM
it would be a pleasure, feel a bit like Neo it`s not the Matrix that`s got me it`s the JavaScript, my daughter has even started bringing my cups of tea to the pc she says i spend more time on it nowbest regards Neil than she does on her phone? scary or what, once again thank you very much i`ll spend all day tommorrow practicing with it

webby07
06-20-2007, 03:35 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>


This prompt each time for monday, tuesday, wednesday, thursday, friday. Have fun programming



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum