2013-02-19 133 views
0

我在使用javascript函数,循环应该运行10次,在这10次中,应该抛出一个基于,+和 - 操作的随机问题,也应该有4个“+”问题,3“ - ”问题和3“”问题。而循环不应运行超过10倍,有人请帧这样的逻辑...循环逻辑

到目前为止我的代码:

<script type="text/javascript"> 
     var op=new Array(); 
     var addCount=0; 
     var subCount=0; 
     var mulCount=0; 
     var counter=0; 
     var no; 
     op[0]="+"; 
     op[1]="-"; 
     op[2]="x"; 

     Array.prototype.chooseRandom = function() 
     { 
      return this[Math.floor(Math.random() * this.length)]; 
     }; 
     var a = [1, 2]; 
     var b = [0, 2]; 
     var c = [0, 1]; 

      no=Math.floor((Math.random()*3)); 
      while(addCount < 4|| subCount < 3 || mulCount < 3) 
      { 

        no=Math.floor((Math.random()*3)); 
        if(no==0) 
        { 
         if(addCount<4) 
         { 
          addCount++; 
      var op1=Math.floor(Math.random() * (99 - 10+1)) + 10; 
       var op2=Math.floor(Math.random() * (99 - 10+1)) + 10; 
         } 
         else 
         { 
          no=a.chooseRandom(); 
         } 
        }  
        else if(no==1) 
        { 
         if(subCount<3) 
         { 
          subCount++; 
      var no1=Math.floor(Math.random() * (99 - 10+1)) + 10; 
      var no2=Math.floor(Math.random() * (99 - 10+1)) + 10; 
          if(no1>no2) 
          { 
           var op1=no1; 
           var op2=no2; 
          } 
          else 
          { 
           var op1=no2; 
           var op2=no1; 
          } 
         } 
         else 
         { 
          no=b.chooseRandom(); 
         } 
        } 
        else if(no==2) 
        { 
         if(mulCount<3) 
         { 
          mulCount++; 
      var op1=Math.floor(Math.random() * (99 - 10+1)) + 10; 
      var op2=Math.floor(Math.random() * (9 - 1+1)) + 1; 
         } 
         else 
         { 
          no=c.choseRandom(); 
         } 
        } 

        counter++; 
      } 
    </script> 
+0

发布你迄今试过的代码 – polin 2013-02-19 07:59:33

+0

@polin,这里的代码,请查看 – Hazel 2013-02-19 08:09:09

回答

0

使含有所需数量的+-*然后数组这阿雷使用下面的函数随机排序:

arr.sort(function() {return 0.5 - Math.random()}) 
+0

为什么0.5 ??你能详细解释一下逻辑吗? – Hazel 2013-02-19 08:06:07

+0

因为排序函数需要返回负值或正值。而随机()返回0到1,这意味着永远是积极的。将它减去0.5,返回的值范围从-0.5到0.5。 – Shivam 2013-02-19 08:19:13

+0

但我不需要负值,您需要在整数运算中使用负​​值吗? – Hazel 2013-02-19 08:27:33

0

希望它能帮助 -

<script type="text/javascript"> 
    function shuffle(o) { 
     for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); 
     return o; 
    } 

    function getQuestions(){ 
     var operators = shuffle(["+", "+", "+", "+", "-", "-", "-", "x", "x", "x"]); 
     var a = [1, 2, 3, 4, 5, 6]; 
     var counter = 0; 

     Array.prototype.chooseRandom = function() { 
      return this[Math.floor(Math.random() * this.length)]; 
     }; 

     while (counter < 10) { 
      var op1 = a.chooseRandom(); 
      var op2 = a.chooseRandom(); 
      alert(op1 + operators[counter] + op2 + "?") 
      counter++; 
     } 
    } 

    getQuestions(); 
</script> 
+0

你能解释一下函数shuffle的用法吗? – Hazel 2013-02-19 09:17:08

+0

@Hazel - shuffle是用来洗牌的运算符数组,所以你可以得到一个随机的运算符数组。 – 2013-02-19 19:27:18