Js Array Input / Output

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

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

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:


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.

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

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.

It seems that an easier to use interface would be to have a drop-down list from which the user can make their choice.


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