var my={} function calcbrokenMain(imode){var version='0.94';this.mode=typeof imode!=='undefined'?imode:'Multiply';console.log("mode",imode,this.mode);var w=390 my.keys=["7","8","9","\u00d7","\u00f7","4","5","6","+","\u2212","1","2","3","(",")","0",".","^","\u2190","C","\u221a","x\u00b2","x\u00b3"," "];my.keyLines=[5,5,5,5,0,4] my.keysn=20 my.keyTypes={0:{lbl:'0',run(){pushU('0');}},pi:{lbl:'pi',run(){pushU('pi');}},x2:{lbl:'x\u00b2',run(){aroundU('(',')^2');}},} my.lvls=[{name:1,tgts:[6,7,8,10,12,15,20,50],keys:[2,3,"+","MUL","C"],time:4},{name:2,tgts:[-10,1,3,10,24,32,100,625],keys:[2,5,"SUB","MUL","(",")","MR","C"],time:3},{name:3,tgts:[-5,3,5,13,20,33,82,100],keys:[1,6,8,"SUB","SQRT","C"],time:5},{name:4,tgts:[3,4,5,6,7,8,9,10],keys:[0,1,2,"MUL","DIV","(",")","C"],time:3},{name:5,tgts:[1,14,24,32,93,100,196,300],keys:[2,4,9,"SUB","x2","(",")","C"],time:5},{name:6,tgts:[1,5,6.5,11,24,32,100,512],keys:[2,"DIV","x2","+","(",")","C"],time:3},] my.lvl=my.lvls[5] var symbs=[['MUL','\u00d7'],['DIV','\u00f7'],['SUB','\u2212'],['SQRT','\u221a'],['x2','x\u00b2'],['x2','x\u00b3']] for(var i=0;i';s+='
';s+='';s+='
 
';s+='
';s+=getKeyHTML();s+='
';s+='
';s+='
' s+='';s+='
';s+=' ';s+='
';s+='
History:
';s+='';s+='
';s+='';s+='';s+='
© 2019 MathsIsFun.com v'+version+'
';s+='';document.write(s);var cw=72 var ch=cw;var div=document.getElementById('timercanvas');var ratio=2;div.width=cw*ratio;div.height=ch*ratio;div.style.width=cw+"px";div.style.height=ch+"px";var gtimer=div.getContext("2d");gtimer.setTransform(ratio,0,0,ratio,0,0);my.timer=new Timer(gtimer,cw/2,60,'#0000ff',gameEnd);my.timer.stop() my.hists=[];my.lastHist="?";my.lastHistTime=-10000;this.p=new Parser();my.tgts=[] this.radsQ=true;toggleRad();var s='' s+='
' s+='
' s+='
' s+=radioHTML('Level','lvl',my.lvls,null) s+='
' my.pop=new Pop('opt','',function(){gameNew()}) my.pop.bodySet(s) my.pop.open() document.getElementById("u").disabled=true;doCalc(false);document.getElementById("u").focus();} function optOpen(){console.log('optOpen') my.opt.open()} function gameEnd(){console.log('gameEnd') var s='' s+='
' s+='Time is Up!' s+='
' var gotN=0 for(var i=0;i',p.rootNode.walkFmt(),'==>',p.getVal(),'==>',p.getVal().fmt());}} function gameNew(){var n=radioGetIndex('lvl',my.lvls) console.log('gameNew',n) my.lvl=my.lvls[n] var tgts=my.lvl.tgts my.tgts=[] for(var i=0;i'+my.tgts[i].val+''}else{s+='
'+my.tgts[i].val+'
'}} document.getElementById('tgts').innerHTML=s} function tgtCheck(num){num=Math.round(num*10)/10 console.log("tgtCheck",num);for(var i=0;i0)s+=lineEnd;if(my.keyLines[row]==0){s+='
' fnQ=true row++} s+=lineStt;} var clr=keyClr(i,'out')+';';if(!fnQ){s+='';}else{s+='';}} s+=lineEnd;return s;} function doKey(i){if(!my.playQ)return var key=my.keys[i] console.log("doKey",i,key);var simples=["1","2","3","4","5","6","7","8","9","0","(",")","+","\u00d7","\u00f7","\u2212",".",'^','i'];if(simples.indexOf(key)>=0){pushU(key);}else{switch(key){case "=":doCalc(true);break;case "C":document.getElementById('u').value='';document.getElementById('a').innerHTML=' ';break;case "1/x":aroundU('(1/(','))');break;case "\u00B1":aroundU('(-(','))');break;case "sin":aroundU('sin(',')');break;case "cos":aroundU('cos(',')');break;case "tan":aroundU('tan(',')');break;case "sin-1":aroundU('asin(',')');break;case "cos-1":aroundU('acos(',')');break;case "tan-1":aroundU('atan(',')');break;case "ex":aroundU('exp(',')');break;case "ln":aroundU('ln(',')');break;case "\u221a":aroundU('sqrt(',')');break;case "x\u00b2":aroundU('(',')^2');break;case "x\u00b3":aroundU('(',')^3');break;case "rad":toggleRad(i);break;case "pi":pushU('pi');break;case "\u2190":var s=document.getElementById('u').value;s=s.substring(0,s.length-1);document.getElementById('u').value=s;break;default:}}} function pushU(s){my.uDiv=document.getElementById('u');if(my.uDiv.selectionStart==0&&my.uDiv.selectionEnd==0){var txt=my.uDiv.value;my.uDiv.setSelectionRange(txt.length,txt.length);} var txt=my.uDiv.value;my.uDiv.value=(txt.substring(0,my.uDiv.selectionStart)+s+txt.substring(my.uDiv.selectionEnd));my.uDiv.setSelectionRange(my.uDiv.selectionStart+s.length,my.uDiv.selectionStart+s.length);doCalc(false);} function aroundU(s1,s2){var s=document.getElementById('u').value;var ops=["+","-","\u2212",'*',"\u00d7",'../index.html',"\u00f7",'^'];for(var i=s.length-1;i>=0;i--){if(ops.indexOf(s.charAt(i))>-1)break;} var sNew='' if(i==0){sNew=s1+s+s2;}else{i++;sNew=s.substr(0,i)+s1+s.substr(i)+s2;console.log("aroundU",s,i,sNew);} document.getElementById('u').value=sNew;doCalc(false);} function userKey(e){var kt=keyType(e.keyCode,e.shiftKey);var f=document.getElementById('u').value;switch(kt){case '=':f=f.substr(0,f.length-1);document.getElementById('u').value=f;doCalc(true);break;case '+':case '*':case '/':case '^':console.log("userKey",kt);if(f.length==1){document.getElementById('u').value=my.hists[0][0]+f;} doCalc(false);break;default:doCalc(false);break;}} function keyType(k,shift){if(k==13)return "=";if(k==61&&shift)return "+";if(k==61&&!shift)return "=";if(k==187&&shift)return "+";if(k==187&&!shift)return "=";if(k==173&&!shift)return "-";if(k==189&&!shift)return "-";if(k==191&&!shift)return "/";if(k==56&&shift)return "*";if(k==106)return "*";if(k==107)return "+";if(k==109)return "-";if(k==111)return "/";return "?";} function doCalc(updateUQ){var f=document.getElementById('u').value;this.p.radiansQ=this.radsQ;this.p.newParse(f);var num=this.p.getVal();if(num==Number.POSITIVE_INFINITY)this.p.errMsg="undefined";if(num==Number.NEGATIVE_INFINITY)this.p.errMsg="undefined";if(this.p.errMsg.length>0){document.getElementById('a').innerHTML=' ';}else{if(updateUQ){document.getElementById('u').value='';} document.getElementById('a').innerHTML=' = '+num;addHist(f,num);} tgtCheck(num)} function addHist(q,a){console.log("addHist",q,a);if(q!=a.toString()){var hist=q+" = "+a;if(hist!=my.lastHist){if(my.lastHistTime+2000>Date.now()){console.log("replace");my.hists[0]=[q,a];}else{console.log("unshift");my.hists.unshift([q,a]);} my.lastHist=hist;my.lastHistTime=Date.now();var h='';for(var i=0;i=3)clr='linear-gradient(to right, #fa8 0%, #fc8 100%)';if(n>=my.keysn)clr='linear-gradient(to right, #fa8 0%, #fc8 100%)';break;case 'over':clr='linear-gradient(to right, #acf 0%, #def 100%)';if(n%5>=3)clr='linear-gradient(to right, #fca 0%, #fdc 100%)';if(n>=my.keysn)clr='linear-gradient(to right, #fca 0%, #fdc 100%)';break;default:} return clr;} function getHist(){var s="";my.lastHistTime=0;for(var i=0;i="0"&&c<="9")||c=='.'){thisType="N";}else{if(this.operators.indexOf(c)>=0){if(c=="-"){thisType="-";}else{thisType="O";}}else{if(c=="."||c==this.Variable){thisType="N";}else{thisType="C";}} if(c=="("){thisType="(";} if(c==")"){thisType=")";}} x+=thisType;if(prevType=="("&&thisType=="-"){y+="0";} if(OpenQ){switch(thisType){case "N":break;default:y+=")";OpenQ=false;}} if(prevType=="O"&&thisType=="-"){y+="(0";OpenQ=true;} y+=c;prevType=thisType;} if(OpenQ){y+=")";OpenQ=false;} return(y);};Parser.prototype.fixImplicitMultply=function(s){var x=s+"\n";var y="";var prevType="?";var prevName="";var thisType="?";var thisName="";for(var i=0;i="0"&&c<="9"){thisType="N";}else{if(this.operators.indexOf(c)>=0){thisType="O";thisName="";}else{thisType="C";thisName+=c;} if(c=="("){thisType="(";} if(c==")"){thisType=")";}} x+=thisType;if(prevType=="N"&&thisType=="C"){y+="*";thisName="";} if(prevType=="N"&&thisType=="("){y+="*";} if(prevType==")"&&thisType=="("){y+="*";} if(thisType=="("){switch(prevName){case "i":case "pi":case "e":case "a":case this.Variable:y+="*";break;}} y+=c;prevType=thisType;prevName=thisName;} return(y);};Parser.prototype.reset=function(){this.tempNode=[];this.errMsg="";};Parser.prototype.parse=function(s){var nnew if(s==""){this.errMsg+="Missing Value\n";return new MathNode("real","0",this.radiansQ);} if(isNumeric(s)){return new MathNode("real",s,this.radiansQ);} if(s.charAt(0)=="$"){if(isNumeric(s.substr(1))){return this.tempNode[Number(s.substr(1))];}} var sLo=s.toLowerCase();if(sLo.length==1){if(sLo>="a"&&sLo<="z"){return new MathNode("var",sLo,this.radiansQ);}} switch(sLo){case "pi":return new MathNode("var",sLo,this.radiansQ);} var bracStt=s.lastIndexOf("(");if(bracStt>-1){var bracEnd=s.indexOf(")",bracStt);if(bracEnd<0){this.errMsg+="Missing ')'\n";return new MathNode("real","0",this.radiansQ);} var isParam=false;if(bracStt==0){isParam=false;}else{var prefix=s.substr(bracStt-1,1);isParam=this.operators.indexOf(prefix)<=-1;} if(!isParam){this.tempNode.push(this.parse(s.substr(bracStt+1,bracEnd-bracStt-1)));return this.parse(s.substr(0,bracStt)+"$"+(this.tempNode.length-1).toString()+s.substr(bracEnd+1,s.length-bracEnd-1));}else{var startM=-1;for(var u=bracStt-1;u>-1;u--){var found=this.operators.indexOf(s.substr(u,1));if(found>-1){startM=u;break;}} nnew=new MathNode("func",s.substr(startM+1,bracStt-1-startM),this.radiansQ);nnew.addchild(this.parse(s.substr(bracStt+1,bracEnd-bracStt-1)));this.tempNode.push(nnew);return this.parse(s.substr(0,startM+1)+"$"+(this.tempNode.length-1).toString()+s.substr(bracEnd+1,s.length-bracEnd-1));}} var k;var k1=s.lastIndexOf("+");var k2=s.lastIndexOf("-");if(k1>-1||k2>-1){if(k1>k2){k=k1;var nnew=new MathNode("op","add",this.radiansQ);nnew.addchild(this.parse(s.substr(0,k)));nnew.addchild(this.parse(s.substr(k+1,s.length-k-1)));return nnew;}else{k=k2;nnew=new MathNode("op","sub",this.radiansQ);nnew.addchild(this.parse(s.substr(0,k)));nnew.addchild(this.parse(s.substr(k+1,s.length-k-1)));return nnew;}} k1=s.lastIndexOf("*");k2=s.lastIndexOf("../index.html");if(k1>-1||k2>-1){if(k1>k2){k=k1;nnew=new MathNode("op","mult",this.radiansQ);nnew.addchild(this.parse(s.substr(0,k)));nnew.addchild(this.parse(s.substr(k+1,s.length-k-1)));return nnew;}else{k=k2;nnew=new MathNode("op","div",this.radiansQ);nnew.addchild(this.parse(s.substr(0,k)));nnew.addchild(this.parse(s.substr(k+1,s.length-k-1)));return nnew;}} k=s.indexOf("^");if(k>-1){nnew=new MathNode("op","pow",this.radiansQ);nnew.addchild(this.parse(s.substr(0,k)));nnew.addchild(this.parse(s.substr(k+1,s.length-k-1)));return nnew;} if(isNumeric(s)){return new MathNode("real",s,this.radiansQ);}else{if(s.length==0){return new MathNode("real","0",this.radiansQ);}else{this.errMsg+="'"+s+"' is not a number.\n";return new MathNode("real","0",this.radiansQ);}}};function MathNode(typ,val,radQ){this.tREAL=0;this.tVAR=1;this.tOP=2;this.tFUNC=3;this.radiansQ=true;this.setNew(typ,val,radQ);} MathNode.prototype.setNew=function(typ,val,radQ){this.radiansQ=typeof radQ!=='undefined'?radQ:true;this.clear();switch(typ){case "real":this.typ=this.tREAL;this.r=Number(val);break;case "var":this.typ=this.tVAR;this.v=val;break;case "op":this.typ=this.tOP;this.op=val;break;case "func":this.typ=this.tFUNC;this.op=val;break;} return(this);};MathNode.prototype.clear=function(){this.r=1;this.v="";this.op="";this.child=[];this.childCount=0;};MathNode.prototype.addchild=function(n){this.child.push(n);this.childCount++;return(this.child[this.child.length-1]);};MathNode.prototype.getLevelsHigh=function(){var lvl=0;for(var i=0;i0){var parq=false;if(this.op=="add")parq=true;if(this.op=="sub")parq=true;if(prevop=="div")parq=true;if(noparq)parq=false;if(this.typ==this.tFUNC)parq=true;if(this.typ==this.tOP){}else{s+=this.fmt(true);} if(parq)s+="(";for(var i=0;i0)s+=this.fmt();s+=this.child[i].walkFmta(false,this.op);if(this.typ==this.tFUNC||(parq&&i>0)){s+=")";}}}else{s+=this.fmt();if(prevop=="sin"||prevop=="cos"||prevop=="tan"){if(this.radiansQ){s+=" rad";}else{s+="\u00b0";}}} return s;};MathNode.prototype.walkNodesFmt=function(level){var s="";for(var i=0;i2){f*=i;} return f;} function isNumeric(n){return!isNaN(parseFloat(n))&&isFinite(n);} class Timer{constructor(g,rad,secs,clr,funcEnd){this.g=g;this.rad=rad;this.secs=secs;this.clr=clr;this.funcEnd=funcEnd;this.x=rad;this.y=rad;this.stt=performance.now();this.stopQ=false;} update(){} restart(secs){this.secs=secs;this.stt=performance.now();this.stopQ=false;requestAnimationFrame(this.draw.bind(this));} more(secs){this.stt+=secs*1000;} stop(){this.stopQ=true;} toString(){var s='' var now=performance.now();var elapsed=now-this.stt;var secs=Math.round(elapsed/1000) var mins=parseInt(secs/60) secs-=mins*60 if(mins>0)s+=mins+'m ' if(secs>0)s+=secs+'s' return s} draw(){if(this.stopQ) return;var now=performance.now();var elapsed=now-this.stt;var ratio=Math.min(1,elapsed/this.secs/1000);var g=this.g;g.beginPath();g.fillStyle="#def";g.arc(this.x,this.y,this.rad,0,2*Math.PI);g.fill();g.beginPath();g.moveTo(this.x,this.y);g.fillStyle=this.clr;g.arc(this.x,this.y,this.rad,-Math.PI/2,ratio*2*Math.PI-Math.PI/2);g.fill();if(ratio<1){requestAnimationFrame(this.draw.bind(this));}else{this.funcEnd();}}} class Pop{constructor(id,yesStr='',yesFunc=null,noStr='',noFunc=null){this.id=id;this.div=document.getElementById(this.id);this.div.style="position:absolute; left:-450px; top:20px; width:auto; padding: 5px; border-radius: 9px; background-color: rgba(125,190,256,0.9); box-shadow: 8px 8px 5px 0px rgba(40,40,40,0.5); transition: all linear 0.3s; opacity:0; text-align: center; ";this.bodyDiv=document.createElement("div");this.div.appendChild(this.bodyDiv);var yesBtn=document.createElement("button");this.div.appendChild(yesBtn);if(yesStr.length<1){yesStr="✔";yesBtn.style='font: 22px Arial;';} yesBtn.innerHTML=yesStr;yesBtn.classList.add('btn');yesBtn.onclick=this.yes.bind(this);var noBtn=document.createElement("button");this.div.appendChild(noBtn);if(noStr.length<1){noStr="✘";noBtn.style='font: 22px Arial;';} noBtn.innerHTML=noStr;noBtn.classList.add('btn');noBtn.onclick=this.no.bind(this);this.yesFunc=yesFunc;this.noFunc=noFunc;return this;} open(){var div=this.div;div.style.transitionDuration="0.3s";div.style.opacity=1;div.style.zIndex=12;div.style.left=10+'px';} yes(me){console.log("me",me);var div=document.getElementById(this.id);div.style.opacity=0;div.style.zIndex=1;div.style.left='-999px';if(typeof this.yesFunc==="function"){this.yesFunc();}} no(){console.log("Pop no");var div=this.div;div.style.opacity=0;div.style.zIndex=1;div.style.left='-999px';if(typeof this.noFunc==="function"){this.noFunc();}} bodySet(s){this.bodyDiv.innerHTML=s;return s;}} function radioHTML(prompt,id,lbls){var s='';s+='
';s+=prompt;for(var i=0;i';s+='';} s+='
';return s;} function radioGetIndex(id,lbls){for(var i=0;i