I am trying to get a user to be prompted with the option to enter a value between 1 and 5, and get access a different array index based on the number they entered.
<h1>JavaScript Arrays</h1>
<p>JavaScript array elements are accessed using numeric indexes (starting from 1).</p>
<h2 id="whichbar"></h2>
<script>
var chocbars = ["Mars Bar","Chokito","Boost","Crunchie","Picnic"];
var userchoice = prompt ('Please enter a value between 1 and 5');
document.getElementById("whichbar").innerHTML = chocbars[4];
</script>
I expect that the user is prompted for a value between 1 and 5, this value then determines which chocolate bar is returned to the screen.
chocbars[userchoice - 1]? Also, array elements are accessed starting from 0, hence the- 1+before the prompt or else the resultant would be a string type.