开发者

if javascript statement something wrong

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


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 .js file for readability:
<script type="text/javascript" src="path to file"></script>.

Tell me if this work for you, too.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        var pos = -1;
        var myGun = new Array;

        function randomSort()
        {
            return parseInt(Math.random() * 10) % 2;
        }

        function roll()
        {
            myGun = [];
            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);
        }

        function shot()
        {
            if(pos < myGun.length)
                document.getElementById("myPos").innerHTML = "Position:" + (pos++) + "<br />";
            else
                alert("you loose");

            return pos;
        }

     </script>
 </head>
 <body>
    <hr />
    <div>
        <div id="myPos"></div>
    </div>

    <hr />
    <!--Storing my array -->
    <div id="myArray">

    </div>

    <br />
    <br />

    <footer>
        <input type="button" id="btnRoll" onclick="roll()" value="Roll" />
        <input type="button" id="btnShot" onclick="shot()" value="Shot" />
    </footer>
</body>
</html>


try this:

function shot(){
        if(pos>=myGun.length)
        {
            pos = pos +1;
            document.getElementById("myPos").innerHTML="Position:"+ pos +"<br/>";

       }
        else
        {
           alert("you loose");
        }
    }


You need to replace ">=" by "<":

if( pos < myGun.length ) { ...

That said, you are also using global variables which is not recommended.

A better way to write this function would be:

function shot( pos, myGun ) {
  if ( pos < myGun.length ) {
    document.getElementById( "myPos" ).innerHTML="Position:"+ ++pos + "<br/>";
  } else {
    alert( "you loose" );
  }
  return pos;
}

Note that the function returns the updated pos, after increment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜