HI Guys!!
My name is Megan and I am a college student in need of some JavaScript help for my computer science class!!
My professor is making us complete some stupid class project for finals and I can’t figure it out! He suggested that we may try to find a JavaScript Website to help us if we have trouble figuring out how to write the code.
I have looked and looked and can’t find out how to fix my code!!
We have to write code to make a slot machine …
I have started the code based on what he gave us and have tried my hardest to figure out how to complete it but I do not know how to finish it.
I have the images working correctly (kind of… the second one seems to have problems loading? I dunno what I did to mess that up) and the counter working, but I need to make it add 13 credits when all 3 images are the same. I added code that I thought would work but it’s not for some reason that I can’t figure out. It should also pop up an alert screen when all 3 are correct and you win. I guess this would have to be including with the if statement and something like alert(‘You win 12 credits!!!’)
Lastly I need to add code that prevents the person from playing when the credit line is 0 (so you can’t get - credits!) and I have no idea how to do this.
Any help would be greatly appreciated!
Here’s what I have so far!
<html>
<head>
<title> Slot Machine</title>
<script type="text/javascript" src="http://balance3e.com/random.js"></script>
<script type="text/javascript">
function SpinSlots()
// Assumes: slot images are in http://balance3e.com/Images
// Resultes: displays 3 random slot images
{
var slot1, slot2, slot3;
slot1 = RandomOneOf(['lemon', 'cherry', 'bar']);
slot2 = RandomOneOf(['lemon', 'cherry', 'bar']);
slot3 = RandomOneOf(['lemon', 'cherry', 'bar']);
document.getElementById('slot1Img').src =
'http://balance3e.com/Images/' + slot1 + '.jpg';
document.getElementById('slot2Img').src =
'http://balance3e.com/Images/' + slot2 + '.jgp';
document.getElementById('slot3Img').src =
'http://balance3e.com/Images/' + slot3 + '.jpg';
document.getElementById('credits').innerHTML =
parseFloat(document.getElementById('credits').innerHTML) - 1;
if (slot1 == slot2 == slot3)
{
document.getElementById('credits').innerHTML =
parseFloat(document.getElementById('credits').innerHTML) + 13;
}
}
</script>
</head>
<body>
<div style="text-align:center">
<p>
<img id="slot1Img" border=1 alt="slot image"
src="http://balance3e.com/Images/cherry.jpg">
<img id="slot2Img" border=1 alt="slot image"
src="http://balance3e.com/Images/lemon.jpg">
<img id="slot3Img" border=1 alt="slot image"
src="http://balance3e.com/Images/bar.jpg">
</p>
<input type="button" value="Click to Spin" onclick="SpinSlots();">
<p>
Credits Remaining: <spin id="credits">20</span>
</p>
</div>
</body>
</html>