Results 1 to 9 of 9

Thread: Array help

  1. #1
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array help

    Hi people!

    Im new to Javascript and need a bit of help. OK, ive got a program, but 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 (i don't want the answer, i just need some direction because im clueless!) Please help!


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

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    after you have created the "days" you need to obtain the date from the server. That will return a number 0-6, which happens to coincide with the 7 days of the week. you will need to modify your script though, to not include 0 and 6 since those represent sat/sun.

  3. #3
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for your help boogyman.

    I only need the days Monday, Tuesday, Wednesday, Thursday, and Friday, and also doesn't the 0 therefore represent wich day you have stated first in the array?

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    yes in your array, but if you are attempting to coincide with an ACTUAL day, you will need to retrieve the date of the server, which will return 0-6 (sun-sat)

    if you are doing it solely for your own purposes then yes, you do not need to worry about that

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm guessing this is a homework problem? If so, there's no need to worry about communicating with the server (if it's a serious project, then as boogyman says, you need to get the date down from the server somehow any not rely on the client's clock). You also want to validate your page (as HTML 4.01 Strict) and avoid using document.write(). You want:
    Code:
    (dayNamesArray[i])
    not
    Code:
    (dayNamesArray + 1)
    too.
    Last edited by Twey; 06-12-2007 at 10:38 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah it is a coursework problem, so based on that, do i still need to change the dayNamesArray bit?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes. I strongly suggest you take my other suggestions too.

    An element of a Javascript array is accessed using the syntax:
    Code:
    myArray[index];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    avoid using document.write()
    what as that been replaced with?

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A new node should be appended to the document containing the desired text.
    Code:
    document.body.appendChild(
      document.createElement("p")
    ).appendChild(
      document.createTextNode("Lion lion lion");
    );
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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