Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: array question

  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array question

    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

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    Code:
    alert(numbers[prompt("enter a number between 1 and 4")-1])

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Homework, surely.

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

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    studying for an assessment next week actually. really useless at javascript so ill no doubt be on here a looooot

    thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •