SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Js Array Input / Output

    Hello people im new to the forums and thought this would be a great way to start my learning curve

    im fiddeling about with javascript and learning the basics.

    say you make a javascript array keeping it simple for just now, say the array had 5 items in it, how can you make it so the user has to input a number or a name which selectes one of the objects from the array ?
    this is the code iv got atm, just simple stuff i have been learning off w3c.

    so what i can imagine a prompt box appears and the user types in ether "1" or "Saab" Which will bring it up on the screen

    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var i;
    
    var myCars=["Saab","Volvo","BMW","Ford", "Astin Martin"]; // literal array
    document.write(myCars[0]);
    
    
    </script>
    
    <form action="">
    please enter number from 1 - 5 <input type="text" name="array" /><br />
    </form>
    
    </body>
    </html>

    Thanks for your input and time
    Kind Regards

    Adam

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by A.mckenzie93 View Post
    so what i can imagine a prompt box appears and the user types in ether "1" or "Saab" Which will bring it up on the screen
    You would check if the value was a number, and if it is a number you would check that it's an integer within the appropriate range for the array.
    If it's not an array you could search the array for the value instead.

    This might be as simple to achieve as:

    Code javascript:
    var car = myCars[Number(value)] || myCars[myCars.indexOf(value)] || '';

    However, I suspect that your purpose is to start learning about JavaScript, so what you will need is likely to require conditional branching and loops loops instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paul thanks for getting back to me,

    im a complete novice and im sure you can tell, i do not understand what it is exactly.

    var car = myCars[Number(value)] || myCars[myCars.indexOf(value)] || '';
    if you have time to explain how i would go about allowing the user to enter a number to show up a array item, for eg il do my best at pseudo code.

    List of Rugby players

    ( array )
    Player 1
    Player 2
    Player 3
    Player 4

    " User please enter player number you wish to pull up "
    user enters the number " 3 "

    document.write("Player3")



    something along those lines if that makes sense, im sorry if this is confusing i just don't really know how to express myself very well when it comes to code.
    if you dont have time to explain this thats ok but if you know of a tutorial you can link me that will teach me this then that's just just as good.

    Thank you so much for your time

    Adam

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by A.mckenzie93
    ......i have been learning off w3c............
    It looks like you might be jumping from one tute to another in an illogical order and so you might be trying to walk before you can run.

    You might be better off going through the more basic js tutorials before playing with arrays and using them.

    Perhaps work through the w3schools javascript tutorials in the order they are presented.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by A.mckenzie93 View Post
    " User please enter player number you wish to pull up "
    user enters the number " 3 "

    document.write("Player3")



    something along those lines if that makes sense
    It seems that an easier to use interface would be to have a drop-down list from which the user can make their choice.

    Code html4strict:
    <form id="choosePlayer" action="showPlayer.html">
        <select name="player">
            <option value="">Please choose...</option>
            <option value="1">Player 1</option>
            <option value="2">Player 2</option>
            <option value="3">Player 3</option>
            <option value="4">Player 4</option>
        </select>
        <input type="submit" value="Show player details">
    </form>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •