<!DOCTYPE html>
<html>
<head>
</head>
<body>
<hr>
<div>
<div id="myPos">
</div>
</div>
<hr>
<!--Storing my array -->
<div id="myArray">
</div>
<br>
<br/>
<hr>
<script type="text/javascript">
var pos=-1;
function randomSort(a,b){
return(parseInt(Math.random()*10)%2);
}
function roll(){
var myGun = new Array();
myGun[0]="First Slot";
myGun[1]="Second Slot";
myGun[2]="Third Slot";
myGun[3]="Fourth Slot";
myGun[4]="Fifth Slot";
myGun[5]="Sixth Slot";
myGun.sort(randomSort).toString();
document.getElementById("myArray").innerHTML="";
for(i=0;i<myGun.length;i++)
{
document.getElementById("myArray").innerHTML+=myGun[i]+ "<br>";
}
document.getElementById("myPos").innerHTML= "Position:"+ (pos=-1);
}
function shot(){
if(pos<myGun.length)
{
document.getElementById("myPos").innerHTML="Position:"+ (pos=pos+1) +"<br/>";
}
else
{
alert("you loose");
}
return pos;
}
</script>
<footer>
<button id="btnRoll" onclick="roll()">Roll</button>
<button id="btnShot" onclick="shot()">Shot</button>
</footer>
</body>
</html>
<!DOCTYPE html> <html> <head> </head> <body> <hr> <div> <div id=myPos> </div> </div> <hr> <!–Storing
Share
Try this out, it worked for me, but I lost. 🙂 I have fixed several minior bugs and made some adjustments to the HTML, which where not the problem. The problem here was the array – which was local variable to the second function, but not to the third – I have made it golobal variable as pos, but reset it in roll (your original definition looked it should be reset there).
The first function not need a and b arguments, since it does not use them.
I have moved all the stuff in the tag of the page, but you can also all script contents it in a separate
.jsfile for readability:<script type="text/javascript" src="path to file"></script>.Tell me if this work for you, too.