JavaScript 24-point game code _javascript tips

Source: Internet
Author: User
Tags abs arithmetic eval
<ptml> <pead> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> 24 Point Game _ Cloud Habitat Community _www.jb51.net </title> <style> Body, td {Font-family: XXFarEastFont-Arial; font-size:9pt; Background-color: #6699FF; Color: #FFFFCC; }. tht {font-family: blackbody; font-size:24pt; color: #000066; Font-weight:bold} input {border: #C6FFFF 1px solid; Ground-color: #003399; Color: #FFFFFF; }. ipk {width:39; height:30 font-size:14pt; Font-weight:bold}. Tit {font-size:14pt; border: #333399 2px solid; BA Ckground-color: #ffffff; Color: #333399; Font-weight:bold}. bg {width:70; height:32;} . xg {width:75; height:30;} #pkbox {width:320; height:110;} #pkbox span {border: #660099 3px solid; Background-color: #ffffff; width:68; height:100; color: #000000; font-size:50px; Text-align:center; Cursor:hand; Padding:16 0; Font-weight:bold; } #sskgxx {border: #000066 2px solid; Background-color: #6666FF; width:130; Height: 180; Color: #ffCCFF; Overflow:auto; Padding:4 7} #k_jg {color: #FF6666; font-size:14pt; font-weight:bold; width:130;} </style> <script language= "JavaScript" >//Program Author: Wave//Date: 2005-8-13 function Lop (j) {if (j>0) {var ss = Lop (j-1); var px = ""; var oo = "$" +j+ "$"; for (var k=0 k<=j; k++) {var re = new RegExp ("(\\d{" +k+ "}) (. *?;)", "G"); px + ss.replace (re,oo); } return px; else return "0;"; The var GT = Lop (3). Slice (0,-1). Split (";"); for (var i=0 i<gt.length; i++) gt[i] = Gt[i].split (""); var ot = []; var op = ["+", "-", "*", "/"]; for (var x=0; x<4; x + +) for (var y=0; y<4; y++) for (var z=0; z<4; z++) {Ot[ot.length] = "((a) +op[x]+ "B" +op[y]+ "C" +op[z]+ "D"; Ot[ot.length] = "(A" +op[x]+ "(b" +op[y]+ "C))" +op[z]+ "D"; Ot[ot.length] = "A" +op[x]+ "((b" +op[y]+ "c)" +op[z]+ "D)"; Ot[ot.length] = "A" +op[x]+ "(b" +op[y]+ "(c" +op[z]+ "D)) "; Ot[ot.length] = "(a" +op[x]+ "b)" +op[y]+ "(c" +op[z]+ "D)"; }///////////////////////////////////////////////////////////////var Ggcs; function Ggcsclass () {this.pjys = 0; THIS.ZQ = 0; THIS.CW = 0; THIS.HL = 0; THIS.GX (); function Ggcsclass.prototype.gx () {k_pjys.innertext = This.zq==0?0:math.round (this.pjys/this.zq*100)/100; K_zq.innertext = THIS.ZQ; K_cw.innertext = THIS.CW; K_hl.innertext = THIS.HL; function Ggcsclass.prototype.phl () {this.hl++; THIS.GX (); K_jg.innertext = "This question is ignored!! "; function GGCSCLASS.PROTOTYPE.PCW () {this.cw++; THIS.GX (); K_jg.innertext = "Wrong!!" "; function Ggcsclass.prototype.pzq () {var d = jcsl.enddate (); This.pjys + = d/1000; this.zq++; THIS.GX (); K_jg.innertext = "Right!" Spents "+math.round (D/10)/100+" Seconds! "; } window.onload = function () {Ggcs = new Ggcsclass (); }///////////////////////////////////////////////////////////////var jcsl=null; Function Jclass () {this.xpa = []; THIS.SSJG = ""; THIS.WX = 1E-10; This.date = null; function Jclass.prototype.rnd () {for (var ii=0 ii<4; ii++) this.xpa[ii] = Math.floor (Math.random () *10) +1; function Jclass.prototype.fapai () {for (var ii=0; ii<4; ii++) document.getElementById ("PK" +ii). innertext = This.xpa[ii]; This.date = new Date (); function Jclass.prototype.endDate () {return new Date ()-this.date; function Jclass.prototype.jw () {var a,b,c,d; This.ssjg= ""; for (var i=0; i<gt.length; i++) {a=this.xpa[gt[i][0]]-0; b=this.xpa[gt[i][1]]-0; c=this.xpa[gt[i][2]]-0; d=this.xpa[gt[i][3]]-0; for (var h=0 h<ot.length; h++) if (Math.Abs (eval (ot[h)) -24) <=this.wx this.jcf (Ot[h].replace ("A", a). R Eplace ("B", b). replace ("C", c). Replace ("D", D)); } function jclass.prototype.jcf (t) {var str = t; var zz1 = t.replace (/[\ (\)]/g, ""); var zz2 = t.replace (/[\ (\)]/g, ""); var zz3 = t.replace (/[\ (\)] (?!) /g, ""); if (Math.Abs (eval (zz1) -24) <=this.wx) str = ZZ1; else if (Math.Abs (eval (zz2) -24) <=this.wx) str = ZZ2; else if (Math.Abs (eval (zz3) -24) <=this.wx) str = ZZ3; var res = str.replace (//g, "") + "=24\n"; if (this.ssjg.indexOf (res) ==-1) THIS.SSJG + = res; function jclass.prototype.scda () {sskgxx.innertext = "all answers: \ n" +THIS.SSJG; function JCLASS.PROTOTYPE.GX (ss) {var SW1 = Ss.match (/\ (/g), SW2 = Ss.match (/\)/g); SW1 = (sw1==null)? 0:sw1.length; SW2 = (sw2==null)? 0:sw2.length; if (SW1!=SW2) return "the number of left and right brackets is not the same"; if (!/^\ (\d+) \) *[\+\-\*\/]\ (* (\d+) \) *[\+\-\*\/]\ (* (\d+) \) *[\+\-\*\/]\ (* (\d+) \) *$/.test (ss)) return "Incorrect arithmetic expression format" ; var str = "#" +regexp.$1+ "|#" +regexp.$2+ "|#" +regexp.$3+ "|#" +regexp.$4+ "|"; for (var ii=0 ii<4; ii++) {var lok = "#" +this.xpa[ii]+ "|"; if (Str.indexof (LOK)!=-1) str = str.replace (Lok, ""); else return "number is wrong in arithmetic expression"; }return "yes"; }//////////////////////////////////////////////////////////////function Fpaasystem () {if (jcsl!=null) GG CS.PHL (); JCSL = new Jclass (); do {jcsl.rnd (); JCSL.JW (); while (jcsl.ssjg== ""); K_jg.innertext = ""; Sskgxx.innertext = ""; Gstext.value = ""; Jcsl.fapai (); function Fpaainput () {var ins = prompt ("Please enter four digits (such as: 5,4,7,10)", ""); if (ins==null) return; if (!/^\d{1,2},\d{1,2},\d{1,2},\d{1,2}$/.test (INS)) {alert ("The input is incorrect!) "); return;} var VF = Jcsl!=null; JCSL = new Jclass (); JCSL.XPA = Ins.split (","); JCSL.JW (); if (jcsl.ssjg== "") {alert ("No solution to this question!!! "); return;} if (VF) GGCS.PHL (); K_jg.innertext = ""; Sskgxx.innertext = ""; Gstext.value = ""; Jcsl.fapai (); function ckda () {if (jcsl==null) return; GGCS.PHL (); JCSL.SCDA (); Jcsl=null; function Okaa () {if (jcsl==null) return; var GS = Gstext.value; var str = JCSL.GX (GS); if (str!= "yes") { Alert ("Formula error: +str+"!) "); Return } if (Math.Abs (eval (GS) -24) <=jcsl.wx) ggcs.pzq (); else GGCS.PCW (); JCSL.SCDA (); Jcsl=null; function TOCs (n) {if (jcsl==null) return; Gstext.value+=jcsl.xpa[n]; } </script> </pead> <body> <span class= "tht" >24 point game </span> Program Author: wave Date: 2005-8-13 & lt;hr> <table > <tr> <td width= "height=" colspan= "2" > Average spents: <span id= "K_pjys" &GT;&L T;/span> sec   Correct: <span id= "K_zq" ></span>  Error: <span id= "K_CW" ></span>  ignored: <span id= "K_hl" ></span> </td> </tr> <tr> <td width= "the" "height=" colspan = "2" > <div id= "Pkbox" > <span id= "pk0" onclick= "TOCs (0)" ></span>  <span id= "PK1" onclick= " TOCs (1) "></span>  <span id=" PK2 "onclick=" TOCs (2) "></span>  <span id=" PK3 "onclick = "TOCs (3)" ></span> </div&Gt </td> </tr> <tr> <td width= "valign=" Top "height=" "> <input type=" button "value=" System Licensing "onclick=" Fpaasystem () "class=" xg "> <input type=" button "value=" Player Selection "onclick=" Fpaainput () "class=" XG "> <input type= "button" value= "View Answer" onclick= "ckda ()" class= "xg" > </td> <td width= "valign=" top Height= > <input type= text size= name= gstext class= > <input type= button value= + "onclick=" Gstext.value+=this.value "class=" IPK "><input type=" button "value="-"onclick=" gstext.value+= This.value "class=" IPK "><input type=" button "value=" * "onclick=" Gstext.value+=this.value "class=" IPK ">< Input type= "button" value= "/" onclick= "Gstext.value+=this.value" class= "IPK" ><input type= "button" value= "(" onclick= "Gstext.value+=this.value" class= "IPK" ><input type= "button" value= ")" onclick= "gstext.value+= This.value "class=" IPK "> <input type=" button"Value=" Back "onclick=" Gstext.value=gstext.value.slice (0,-1) "class=" BG "><input type=" button "value=" clear " onclick= "gstext.value=" "class=" BG ">    <input type=" button "value=" determines "onclick=" Okaa () "class=" BG > </td> </tr> </table> <div id= "sskgxx" ></div> <div id= "K_JG" ></div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.