Results 1 to 3 of 3

Thread: using user inputs from prompt boxes

  1. #1
    Join Date
    Apr 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using user inputs from prompt boxes

    ive created a dice game. The idea is simple. there are three images. two are of 6 sided dice and the other image reads "roll the dice". you click roll the dice and two random dices images are displayed, for example a two and 5 making 7. you can keep clicking roll the dice and the images will change each time. I want to add a prompt box where a user is to guess the outcome of the dice, this i know how to do, however this is where my problem comes in... if the user guesses 7 and the dice rolls anything other than 7 i want it to display a sorry you lose message. and if the user guesses right i want a congratulations you win message. i have seperated my javascript from my html page. this is the code i have so far.

    HTML

    Code:
    <html>
        <head>
            <title>The Dice Game
            </title>
        </head>
    <body>
    
    <img src="dice1.jpg" id="di1" />
    <img src="dice2.jpg" id="di2" />
    <img src="roll.jpg" id="dice" title="roll" 	onclick="rolldice(); return false;"/>
    
    <script type="text/javascript" src="dice.js">
    </script>
    
    </body>
    and the javascript is...
    Code:
    var image =[
            "dice1.jpg",
            "dice2.jpg",
            "dice3.jpg",
            "dice4.jpg",
            "dice5.jpg",
            "dice6.jpg",
    ];
    
    function rolldice()
    {
        var dice = Math.floor(Math.random() * (image.length+1));
        document.getElementById("di1").src = image[dice];
        
        var dice = Math.floor(Math.random() * (image.length+1));
        document.getElementById("di2").src = image[dice];
    }
    
    var guess = prompt("What is your guess?")
    any help with how to do this would be greatly appreciated.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    here:
    Code:
    var image =[
            "dice1.jpg",
            "dice2.jpg",
            "dice3.jpg",
            "dice4.jpg",
            "dice5.jpg",
            "dice6.jpg",
    ];
    
    function rolldice()
    {
        var dice = Math.floor(Math.random() * (image.length+1));
        document.getElementById("di1").src = image[dice];
        var total = image[dice].replace(/dice([1-9])\.jpg/i, "$1");
    
        var dice = Math.floor(Math.random() * (image.length+1));
        document.getElementById("di2").src = image[dice];
        total += image[dice].replace(/dice([1-9])\.jpg/i, "$1");
        if (total == guess) {
            alert("You guessed write!");
        }
    }
    var guess = prompt("What is your guess?");
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Apr 2008
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I would use an if/else statement for the wrong/right thing. Something like this:

    Code:
    if (guess == total) 
    {
      alert("Congratulations, you win.");
    }
    else 
    {
      alert("Sorry, you lose.");
    }
    You may have to parseFloat guess first. That's the idea, though.

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
  •