PDA

View Full Version : using user inputs from prompt boxes



fredw
04-10-2008, 07:08 PM
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



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


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.

Master_script_maker
04-10-2008, 07:51 PM
here:


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?");

Pseudoquazi
04-10-2008, 08:07 PM
I would use an if/else statement for the wrong/right thing. Something like this:



if (guess == total)
{
alert("Congratulations, you win.");
}
else
{
alert("Sorry, you lose.");
}


You may have to parseFloat guess first. That's the idea, though.