html - javascript to display user defined element from an array -


i need make simple javascript code display element array of fruits containing 5 elements condition fruit must user defined index of fruit must user supplied , javascript print that.

text area must user input , on submission displays element

there many ways this, perhaps simple use prompt box.

var fruits = ["fruit1", "fruit2", "fruit3", "fruit4"];    var choice = prompt("please enter number between 0 , " + fruits.length);    // check not null  if(choice)  {    //convert integer    parseint(choice);    alert(fruits[choice]);    //or    console.log(fruits[choice])  }

obviously should handle validation on string make sure integer!


Comments