Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Array's

  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array's

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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>
    Last edited by Philip M; 06-14-2007 at 11:58 AM. Reason: Noted another typo - nuber!

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Morning Philip, you my friend are an absolute genius, have a good day

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    <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!
    Last edited by Philip M; 06-14-2007 at 10:24 PM.

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    New Coder
    Join Date
    Jun 2007
    Location
    scriptology city
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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
    scriptology


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •