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 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random from array

    ok, i'm trying to make a deck of cards
    Code:
    var decksuitsk=new Array(4)
    decksuits[0]="Spades"
    decksuits[1]="Hearts"
    decksuits[2]="Clubs"
    decksuits[3]="Diamonds"
    
    var decknumber=new Array(13)
    decknumber[0]="Ace"
    decknumber[1]="2"
    decknumber[2]="3"
    decknumber[3]="4"
    decknumber[4]="5"
    decknumber[5]="6"
    decknumber[6]="7"
    decknumber[7]="8"
    decknumber[8]="9"
    decknumber[9]="10"
    decknumber[10]="Jack"
    decknumber[11]="Queen"
    decknumber[12]="King"
    how would I go about getting a random one out of each of the arrays?
    Last edited by the_bob; 06-05-2004 at 05:40 PM. Reason: typos...

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    Array.prototype.getRandom = function()
    {
    	return this[Math.floor(Math.random()*this.length)];
    }
    Then simply:
    Code:
    var foo = decksuitsk.getRandom(); // foo now contains a random element from the decksuitsk array

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i must admit, i've NEVER worked with arrays before

    is there a way to turn the 2 randoms into variables...?
    Code:
    alert("You drew the " + (randomdecknumber) + " of " + (decksuit))
    or did you just tell me how to do that?
    Last edited by the_bob; 06-06-2004 at 01:13 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by the_bob
    or did you just tell me how to do that?
    If I understand what you're asking, yes. All I've done above is added a method onto all arrays that allows you to quickly access a random element from it. When you call this method it returns the contents of that random element. Thus, by assigning the return value to a variable you then get a random element from the array stored in the variable.

  • #5
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    <title>Cards</title>
    <script>
    var decksuitsk=new Array(4)
    decksuits[0]="Spades"
    decksuits[1]="Hearts"
    decksuits[2]="Clubs"
    decksuits[3]="Diamonds"
    
    var decknumber=new Array(13)
    decknumber[0]="Ace"
    decknumber[1]="2"
    decknumber[2]="3"
    decknumber[3]="4"
    decknumber[4]="5"
    decknumber[5]="6"
    decknumber[6]="7"
    decknumber[7]="8"
    decknumber[8]="9"
    decknumber[9]="10"
    decknumber[10]="Jack"
    decknumber[11]="Queen"
    decknumber[12]="King'
    
    Array.prototype.getRandom = function()
    {
    	return this[Math.floor(Math.random()*this.length)];
    }
    
    var foo = decksuitsk.getRandom();
    </head>
    <body>
    <script>
    
    alert(foo);
    
    </script>
    </body>
    </html>
    would it be set up like that?

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basically, except your code has several fatal errors. I also prefer using the literal array notation.
    Code:
    Array.prototype.getRandom = function() { return this[Math.floor(Math.random()*this.length)]; }
    
    var
    	deckSuits = ['Spades', 'Hearts', 'Clubs', 'Diamonds'],
    	deckNumber = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'],
    	foo = deckSuits.getRandom();
    
    alert(foo);

  • #7
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    <title>Cards</title>
    <script>
    
    
    Array.prototype.getRandom = function() { return this[Math.floor(Math.random()*this.length)]; }
    
    var
    	deckSuits = ['Spades', 'Hearts', 'Clubs', 'Diamonds'],
    	deckNumber = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'],
    	foo = deckSuits.getRandom();
    
    
    
    </head>
    <body>
    <script>
    
    alert(foo);
    
    </script>
    </body>
    </html>
    hmm... where are these fatal errors at?

  • #8
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OH! I GOT IT WORKING!
    thanx man, i really appreciate this

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Your fatal error is that you forgot the end tag for the script element in the head, for what I can see.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    Your fatal error is that you forgot the end tag for the script element in the head, for what I can see.
    That, and:
    Code:
    decknumber[12]="King'
    Starts with a double quote yet ends with a single quote.


  •  

    Posting Permissions

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