• 当前位置:首页>>JavaScript教程>>JavaScript基础教程>>javascript基础实例:如何计算24点(2)
  • javascript基础实例:如何计算24点(2)
  •     function test(expn,a,b,c,d,m1,m2,m3)
        {
         var exp;

         var ret;
         exp = genExpress(expn,a,b,c,d,m1,m2,m3);//生成计算表达式
         eval("ret = "+exp);
         if ( ret - 24 < 0.1 && ret - 24 >= 0 )
         {
          exp = exp.replace(";","");
          exp = replaceAll(exp,"*","×");
          exp = replaceAll(exp, "/","÷");
          var have = false;
          for ( var i=0; i<counter; i++)
          {
               if ( exp == answer[i] )
       {
            have = true;
            break;
           }
          }
          if ( !have )
          {
           answer[counter] = exp;
           counter++;
           log("<font color=red><b>"+counter+":&nbsp;&nbsp;"+exp+"</b></font>");
          }

         }
        }
       
        function replaceAll (streng, soeg, erstat)
        {
         var st = streng;
         if (soeg.length == 0)
          return st;
         var idx = st.indexOf(soeg);
         while (idx >= 0)       
         { 
          st = st.substring(0,idx) + erstat + st.substr(idx+soeg.length);
          idx = st.indexOf(soeg);
         }
         return st;
        }

            var n = new Array();//四个数字
       
        //接收四个输入框的数字,调用主程序
        function funCount()
        {
         n[0] = document.forms[0].fa.value;
         n[1] = document.forms[0].fb.value;
         n[2] = document.forms[0].fc.value;

     

    [1]

  • 上一篇:javascript对象与数组参考大全(15)
    下一篇:javascript基础实例:如何计算24点(3)