Thread: Resolved Array
View Single Post
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,200
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)