SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays and Prompt

    I'm doing a deck of cards javascript code.

    What I want: Ask the user to pick a card between 1-52.
    What I have to so far: is 0-51...how would I kick that up so that I get the 0 out of the way.

    <script language="javascript" type="text/javascript">


    var name = prompt("Choose a Card between 1 and 52",0);
    var card = new Array(51);

    //Cards Name, Suit, Value and Display from ace to king
    card[0] = new Array("Ace","Club","1","\u2663");
    card[1] = new Array("Ace","Spade","1","\u2660");
    card[2] = new Array("Ace","Heart","1","\u2665");
    card[3] = new Array("Ace","Diamond","1","\u2666");

    ^^These go all the way up to the king[51]^^

    document.write("Card Name : " + card[name][0] + "<br>");
    document.write("Suit : " + card[name][1] + "<br>");
    document.write("Value : " + card[name][2] + "<br>");
    document.write("Symbol : " + card[name][3] + "<br>");

    Everything works, I just want to know how I make it so that card [0] will be card 1 when the user types it in.

    Thanks, Joe

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I have to so far: is 0-51...how would I kick that up so that I get the 0 out of the way.
    Add 1.

    Everything works, I just want to know how I make it so that card [0] will be card 1 when the user types it in.
    Subtract 1.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry but your gonna have to tell me where in the code to put it...

    I know I have to do a -1 somewhere.

    I got 0 to 51, thats 52 arrays I just want the numbers to match so that when the user types in 1 he gets card[0]

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got 0 to 51, thats 52 arrays
    What size array is created by the following:

    var card = new Array(51);

    Hint: the specified number is not the highest index value.

    I just want the numbers to match so that when the user types in 1 he gets card[0]
    If the user enters 1 and you read it into the variable 'name', and you want card[name] to be card[0], what do you have to do to 'name'? In other words, what mathematical operation do you have to perform on 1 to make the result equal to 0?


Bookmarks

Posting Permissions

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