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 5 of 5

Thread: picka card

  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    picka card

    Hi,

    Can anyone tell me how to do this - or point me in the direction of some code that is close.

    I want the user of a website to be able to click on a button called, for example, 'pick' and the code to return to the screen with a randomly chosen jpeg from a finite list along with some text describing it. For example 1 out of 52 jpegs representing a full pack of cards alongside the text 'Jack of Clubs', Or a random photo from my photo gallery as well as the text 'Pretty Picture'.

    Any help gratefully received

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Provo, UT
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is some code to get you started. I didn't test it so you may have to play with it but it will get you started.

    Code:
    //first get a randomly generated number
    function RandomNumber(){
    return Math.ceil(Math.random()*deckJpgArray.length);
    }
    
    //have an array of jpeg paths
    deckJpgArray = new Array();
    
    deckJpgArray[0] = 'pathto/acehearts.jpg';
    deckJpgArray[1] = 'pathto/twohearts.jpg';
    deckJpgArray[2] = 'pathto/threehearts.jpg';
    //etc for the whole deck
    
    //do the picking and displaying
    
    function pickCard(){
    document.getElementById('cardDisplay').innerHTML = '<img src="'+deckJpgArray[RandomNumber()]+'"/>';
    }
    the html would be something like

    Code:
    <div id="cardDisplay"></div>
    <div><input type="button" onclick="pickCard();" value="pick"/></div>
    I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much - thats excellent


    Andy

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Provo, UT
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did it really work?

    I wrote it really quick and didn't check if it had any bugs. I would be careful with it.

    One thing you should check that I am worried about is if the first and the last card in the deck come up just as much as the other. That is always the tricky part of doing something like this. I did Math.ceil and it may have to be math.floor.

    I am glad it helped though. that is awesome.
    I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Math.floor would be the one to use for arrays
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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