Reputation: 11
I'm studying for an upcoming exam and i'm having trouble with the not a number (NaN)error, I feel like its going to be something simple but i cant seem to find it, also my lecturer is very picky so a solution simple or small would be perfect.
Basically i have a simple craps game which i'm using to help study for a similar exam coming up, two dice if they equal 7 its a win, if not a loss, this part was fine until i added in a simple betting system.The error appears in the money (span id="money") section once the go button is pressed(The random number/dice part is fine), i'm pretty sure its in the winning/losses section
Any help at all would be appreciated thanks.
<html>
<head>
<title>Craps Game</title>
</head>
<body>
<img src="dice1.png" name="firstdice" id="one" width="100" height="100" alt="dicea" />
<img src="dice2.png" name="seconddice" id="two" width="100" height="100" alt="diceb" />
<button type="button" onclick="toss()">Go</button>
<p><b>Money: </b><span id="money">10</span></p>
<p><b>Bet: </b><br/><textarea id="bet" style="text-align:center;">1</textarea></p>
<h2 id="sometext">B</h2>
</body>
<script type="text/javascript">
function toss()
{
var winnings = 0;
var loss = 0;
//Dice
var random1 = Math.floor(Math.random() * 6) + 1;
var random2 = Math.floor(Math.random() * 6) + 1;
var total = random1+random2;
firstname = "dice" + random1 + ".png";
secondname= "dice" + random2 + ".png";
document.getElementById('one').src=firstname;
document.getElementById('two').src=secondname;
var money = parseInt(document.getElementById("money").value);
var bet = parseInt(document.getElementById("bet").value);
//winnings
if (total == 7)
{
winnings = bet*2;
sometext.innerHTML = "You Win!";
}
money += winnings;
document.getElementById('money').innerText = money
//losses
if(total != 7)
{
loss = money-bet
sometext.innerHTML = "You Lose";
}
document.getElementById('money').innerText = loss
}
</script>
</html>
Upvotes: 0
Views: 141
Reputation: 21565
<span id="money">10</span>
does not have a .value
attribute. You will want to use .innerHTML
:
var money = parseInt(document.getElementById("money").innerHTML);
Upvotes: 6