Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2012, 04:57 AM   PM User | #1
galaxiesanddust
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
galaxiesanddust is an unknown quantity at this point
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..
galaxiesanddust is offline   Reply With Quote
Old 12-08-2012, 05:54 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
galaxiesanddust (12-08-2012)
Old 12-08-2012, 06:28 AM   PM User | #3
galaxiesanddust
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
galaxiesanddust is an unknown quantity at this point
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.
galaxiesanddust is offline   Reply With Quote
Old 12-08-2012, 09:48 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 12-08-2012 at 09:53 AM..
AndrewGSW is offline   Reply With Quote
Old 12-08-2012, 08:52 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-08-2012, 09:05 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:40 PM.


Advertisement
Log in to turn off these ads.