我正在使用 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>