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

Thread: Array

  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Array

    The code that I have to work on is supposed to create an array so that when the user enters the number when prompted, it matches and displays the correct president. Right now, the prompt just continues and it ends with the whole list being displayed.

    If anyone could give me some assistance with this, it would be greatly appreciated.

    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    // Program name: presidents.html
    // Purpose: Creates an array for users to enter
    // a number that matches with the correct President
    // Author: Paul Addison
    // Date last modified: 01-Sep-2011
    
    // Declare variables and constants
    var SIZE = 44;             // array size
    var presidents;            // array for book titles
    var index;                 // loop index
    var ES = "";               // empty string
    var BR = "<br />";         // HTML line break
    
    // Create presidents array
    presidents = new Array(SIZE);
    
    // Prompt user to enter a number
    for (index = 0; index < SIZE; index++) {
    presidents[index] = prompt("Enter a number" + ":",ES);
    presidents[1]="Washington";
    presidents[2]="Adams";
    presidents[3]="Jefferson";
    presidents[4]="Madison";
    presidents[5]="Monroe";
    presidents[6]="Adams";
    presidents[7]="Jackson";
    presidents[8]="Van Buren";
    presidents[9]="Harrison";
    presidents[10]="Tyler";
    presidents[11]="Polk";
    presidents[12]="Taylor";
    presidents[13]="Filmore";
    presidents[14]="Pierce";
    presidents[15]="Buchanan";
    presidents[16]="Lincoln";
    presidents[17]="Johnson";
    presidents[18]="Grant";
    presidents[19]="Hayes";
    presidents[20]="Garfield";
    presidents[21]="Arthur";
    presidents[22]="Cleveland";
    presidents[23]="Harrison";
    presidents[24]="Cleveland";
    presidents[25]="McKinley";
    presidents[26]="Roosevelt";
    presidents[27]="Taft";
    presidents[28]="Wilson";
    presidents[29]="Harding";
    presidents[30]="Coolidge";
    presidents[31]="Hoover";
    presidents[32]="Roosevelt";
    presidents[33]="Truman";
    presidents[34]="Eisenhower";
    presidents[35]="Kennedy";
    presidents[36]="Johnson";
    presidents[37]="Nixon";
    presidents[38]="Ford";
    presidents[39]="Carter";
    presidents[40]="Reagan";
    presidents[41]="Bush";
    presidents[42]="Clinton";
    presidents[43]="Bush";
    presidents[44]="Obama";
    }
    
    // Display president
    document.write("President:" + BR + BR);
    for (index = 0; index < SIZE; index++) {
       document.write(presidents[index] + BR);
    }
    
    // Thank the user and end the program
    document.write("Thank you." + BR);
    </script>
    </body>
    </html>
    Last edited by galaxiesanddust; 12-08-2012 at 06:30 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    lots of mistakes.

    And you really shouldn't be using prompt() or document.write() at all. Those are both considered obsolete.

    Plus, because your code uses document.write, there's no good way to allow the user to enter a number more than once.

    Anyway, for starters, kill you current method of creating the array. K.I.S.S.

    Code:
    var presidents = [ "N/A", "Washington", "Adams", "Jefferson", ... ];
    var which = ...get a number from user ...
    var choice = presidents[which};
    ...
    Try to do it using a <form> and an <input> instead of prompt(), if you can.

    And then put the choice in place using innerHTML or put it into another <form> field using .value.

    Oh, and there is no need for any FOR loop in your code, even if you don't use what I suggested. Dunno why you ever thought you needed one.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    galaxiesanddust (12-08-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, that helped a lot.

    I know that a lot of the coding I've had to use isn't the way I should be doing it. But, my class is supposed to be going by the codes that are in our book (which has been super vague in my opinion)and I've been told by multiple people that they're obsolete. So, I've been having some trouble trying to stay in the guidelines of the book. I'm definitely starting to feel like it would just be better to do what I can in a more updated way instead of like this.

    But, again I do appreciate the help.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Slightly off topic (Sorry!) but there is a very interesting article about JS Arrays here.

    If you are a JS enthusiast (as I am) then consider javascriptweekly.com.
    Last edited by AndrewGSW; 12-08-2012 at 09:53 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Quote Originally Posted by galaxiesanddust View Post
    my class is supposed to be going by the codes that are in our book
    YOu aren't alone. It seems that way way too many schools use books that are obsolete. And the sad part about it is that many of these books are relatively new! One published in late 2011 was still teaching document.write() and alert() as acceptable coding techniques.

    So long as students don't *DEMAND* that their schools teach them *MODERN* coding, then the schools will continue to allow lazy and incompetent instructors to choose to teach old style coding. So long as the instructors and schools don't DEMAND the publishers produce modern books, the publishers will continue to issue total crap.

    I wish you luck.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by galaxiesanddust View Post
    Thanks, that helped a lot.

    I know that a lot of the coding I've had to use isn't the way I should be doing it. But, my class is supposed to be going by the codes that are in our book (which has been super vague in my opinion)and I've been told by multiple people that they're obsolete. So, I've been having some trouble trying to stay in the guidelines of the book. I'm definitely starting to feel like it would just be better to do what I can in a more updated way instead of like this.

    But, again I do appreciate the help.
    Sounds like your book is teaching how JavaScript used to be written back in the Netscape days and you'll need to start over again if you want to learn how to write JavaScript for more modern browsers such as IE5+.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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