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

Thread: Array help

  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array help

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,140
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Hint: JavaScript is case sensitive. Check your spelling of Day/day.

    No semi-colon after }

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help, i made the amendments you suggested, but i am still getting day 1 day 2 etc etc

  • #4
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #5
    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>

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