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