...

View Full Version : array question



JSbeginner1
12-09-2009, 01:50 PM
hi, if i have an array of "hop", "skip", "jump" "run", how do i prompt the user to enter a number between 1 and 4 to bring back one of the array list without using an if or switch statement...

the code i have so far is:

numbers= new Array();
numbers[0]="hop";
numbers[1]="skip";
numbers[2]="jump";
numbers[3]="run";

thanks

gusblake
12-09-2009, 02:19 PM
alert(numbers[prompt("enter a number between 1 and 4")-1])

Philip M
12-09-2009, 02:23 PM
Homework, surely.


numbers= new Array();
numbers[0]="hop";
numbers[1]="skip";
numbers[2]="jump";
numbers[3]="run";

var which = prompt ("Enter a number from 1 -4","");
// you need additional validation here to ensure that the value entered is a number in range 1-4
which = which -1;
alert (numbers[which]);


Quizmaster: In Shakespeare's Hamlet, a famous quotation is "Get thee to a ......" what?
Contestant: Church on time.

JSbeginner1
12-09-2009, 02:28 PM
studying for an assessment next week actually. really useless at javascript so ill no doubt be on here a looooot

thanks for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum