...

View Full Version : Resolved Array



galaxiesanddust
12-08-2012, 04:57 AM
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.


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

Old Pedant
12-08-2012, 05:54 AM
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.



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.

galaxiesanddust
12-08-2012, 06:28 AM
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.

AndrewGSW
12-08-2012, 09:48 AM
Slightly off topic (Sorry!) but there is a very interesting article about JS Arrays here (http://www.2ality.com/2012/12/arrays.html?utm_source=javascriptweekly&utm_medium=email).

If you are a JS enthusiast (as I am) then consider javascriptweekly.com.

Old Pedant
12-08-2012, 08:52 PM
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.

felgall
12-08-2012, 09:05 PM
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+.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum