';document.write(s);dragType='';my.clrs=[["Blue",'#0000FF'],["Red",'#FF0000'],["Black",'#000000'],["Green",'#00cc00'],["Orange",'#FFA500'],["Slate Blue",'#6A5ACD'],["Lime",'#00FF00'],["Spring Green",'#00FF7F'],["Teal",'#008080'],["Gold",'#ffd700'],["Med Purple",'#aa00aa'],["Light Blue",'#ADD8E6'],["Navy",'#000080'],["Purple",'#800080'],["Dark SeaGreen",'#8FBC8F']];clrNum=0;parser=new Parser();my.qs=qsLoad();my.names=namesGet();my.things=thingsLoad();qNext()}
function ontouchstart(evt){let touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;onmouseDown(evt)}
function ontouchmove(evt){let touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;onmouseMove(evt);evt.preventDefault();}
function ontouchend(evt){el2.addEventListener('touchstart',ontouchstart,false);window.removeEventListener("touchend",ontouchend,false);}
function onmouseDown(evt){if(my.resultQ)return;let bRect=el2.getBoundingClientRect();let mouseX=(evt.clientX-bRect.left);let xVal=numlineVal(mouseX);choose(xVal);if(evt.preventDefault){evt.preventDefault();}
else if(evt.returnValue){evt.returnValue=false;}
return false;}
function onmouseMove(evt){if(my.resultQ)return;let bRect=el2.getBoundingClientRect();let mouseX=(evt.clientX-bRect.left);let mouseY=(evt.clientY-bRect.top);let xVal=numlineVal(mouseX);drawNumLine(g2,50,30,0,10,mouseX,mouseY);}
function numlineVal(mousex){return coords.toXVal(mousex-50);}
function calcScore(diff){let pct=Math.abs(diff/(my.game.num[1]-my.game.num[0]))*100;if(pct<=2){return 5;}else{if(pct<=4){return 4;}else{if(pct<=7){return 3;}else{if(pct<=10){return 2;}else{if(pct<=15)return 1;}}}}
return 0;}
function fancyText(s,x,y){let clr=g.fillStyle;g.fillStyle='#fff';let d=1;g.fillText(s,x-d,y-d);g.fillText(s,x-d,y+d);g.fillText(s,x+d,y+d);g.fillText(s,x+d,y-d);g.fillStyle=clr;g.fillText(s,x,y);}
function qNext(){document.getElementById('msg').innerHTML=''
my.q=qGet();if(my.q==null){return}
my.numMax++
my.numMax=Math.min(my.numMax,20)
console.log('my.numMax',my.numMax)
document.getElementById('quest').innerHTML=my.q.txtStr
let div=document.getElementById('method')
div.style.visibility='hidden'
div.innerHTML=my.q.methodStr
document.getElementById('nextBtn').style.visibility='hidden'
anssGet(my.q)
console.log("qNext",my.q);let i=0
my.choices.map(choice=>{let div=document.getElementById(choice.id)
div.innerHTML=my.q.anss[i++]
div.classList.remove("lo")
div.classList.remove("hi")})}
function ansClick(id){let idNo=parseInt(id.substr(3))
console.log('ansClick',id,idNo)
if(idNo==my.q.ansNo){document.getElementById(id).classList.add("hi")
let div=document.getElementById('msg')
div.style.color='gold'
div.innerHTML='Well done!'
my.score+=5
document.getElementById('score').innerHTML=my.score
document.getElementById('method').style.visibility='visible'
document.getElementById('nextBtn').style.visibility='visible'}else{document.getElementById(id).classList.add("lo")
let div=document.getElementById('msg')
div.style.color='#888'
div.innerHTML='Whoops, that was the wrong answer, try again ...'
my.score-=2
my.score=Math.max(0,my.score)
document.getElementById('score').innerHTML=my.score}}
function qsLoad(){let qs=[];qs.push([2,"y-x",["y>x","Cups:give"],"You chose [x] Cupsx and your friend chose [y] Cupsx. How many more Cupsx did your friend choose?","[y] - [x] = [y-x]"]);qs.push([1,"x+y",["x>2","Cups:give"],"Betty gives John [x] Cupsx. John already had [y] Cupsy. So how many does John have now?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);qs.push([2,"y-x",["y>6","y>x"],"In your class today, only [x] of the [y] students were at school. How many were absent?","[y] - [x] = [y-x]"]);qs.push([2,"x-y",["x>y","Cups:money"],"You saved [x] Cupsx and your brother saved [y] Cupsy. How many more Cups did you save?","[x] - [y] = [x-y]"]);qs.push([2,"x-y",["x>2","x>y","y>2"],"You have [x] birthday gifts! [y] came from your family, the rest came from your friends. How many gifts did your friends give you?","[x] - [y] = [x-y]"]);qs.push([2,"y-x",["y>x","x>2","Cups:give"],"Your friends just gave you [x] Cupsx, now you have [y]. How many did you have before your friends gave you them?","[y] - [x] = [y-x]"]);qs.push([1,"x+y",["y>1","x>1","Cups:gift"],"John got [x] new Cups on his birthday. If he already had [y], how many Cups does he have now?","[x] + [y] = [x+y]"]);qs.push([1,"x-y",["y>1","x>y","Cups:eat"],"John ate [x] Cups and Betty ate [y] Cups. How many more Cups did John eat than Betty?","[x] - [y] = [x-y]"]);qs.push([1,"3x",["x>1"],"How many corners are on [x] triangles?","[x] × 3 = [x*3]"]);qs.push([1,"4x",["x>1"],"How many corners are on [x] squares?","[x] × 4 = [x*4]"]);qs.push([3,"y",["x>3","y>x","Cups:eat"],"John is preparing Cupsx for the tennis event. He has already prepared [x] Cupsx, but needs [x+y]. How many more Cupsx does he need?","[x+y] - [x] = [y]"]);qs.push([1,"x-y",["x>y","y>1","Cups:clr"],"John has [x] Cupsx. [y] are green and the rest are blue. How many Cups are blue?","[x] Cupsx - [y] Cupsy = [x-y] Cups"]);qs.push([1,"y-x",["x>2","y>x","Cups:give"],"John has just been given [x] Cupsx, he now has [y] Cupsy. How many did he have before?","[y] Cupsx - [x] Cupsy = [y-x] Cups"]);qs.push([1,"x+y",["x>2","Cups:give"],"John lost [x] Cupsx, and now has only [y] Cupsy. How many did he have before?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);qs.push([1,"x+y",["x>2","Cups:give"],"John had [x] Cupsx and was given [y] more, how many Cups does he have now?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);qs.push([1,"x+y",["x>2","Cups:give"],"John has [x] Cupsx and plans to get [y] more tomorrow. How many will he have then?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);qs.push([1,"x+y",["x>2","Cups:money"],"On Monday John earned [x] Cupsx. On Tuesday he earned [y] Cupsy. How much did he earn on the two days?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);qs.push([3,"y",["x=30","y>2","y<10"],"It takes [x*y] days to save enough money for my vacation. About how many months is that?","[x*y] ÷ [x] = [(x*y)/x]"]);qs.push([3,"y",["x>6","x<20","y>10","y<30","Cups:give"],"John has [x*y] Cupsx to share among [x] friends. How many Cupsx will each of them receive?","[x*y] ÷ [x] = [y]"]);qs.push([2,"y-x",["x>2","y>x","Cups:give"],"John just gave you [x] Cupsx, now you have [y]. How many did you have before he gave you [x] more?","[y] Cupsy - [x] Cupsx = [y-x] Cups"]);qs.push([1,"x+y",["x>2","Cups:give"],"John had [x] Cupsx and was given [y] more, how many Cups does he have now?","[x] Cupsy + [y] Cupsx = [x+y] Cups"]);let qobjs=[];for(let i=0;i=my.qPrevs.length-notInLast;i--){if(qNo==my.qPrevs[i]){okQ=false
break}}}while(!okQ)
my.qPrevs.push(qNo)
let q=qs[qNo];if(q==null)return null;let qStr=q.txt;let methodStr=q.method;if(methodStr==null){methodStr="";}
vals=[];let GTs=[0,0,0];let maxP1=my.numMax+1
let LTs=[maxP1,maxP1,maxP1];let anss=[];let attempts=0;let OKQ=false
let Name1,Name2
do{for(i=0;i<3;i++){vals[i]=randomInt(GTs[i]+1,LTs[i]-1);}
OKQ=true;for(i=0;i","="]);if(condarr.length==3){let lhs=evalExpr(condarr[0],vals);let condtype=condarr[1];let rhs=evalExpr(condarr[2],vals);if(condarr[0]=="a"){anss.push(condarr[2]);}else{let n=varNo(condarr[0]);if(n>=0){switch(condtype){case ">":GTs[n]=rhs;LTs[n]=Math.max(LTs[n],GTs[n]+2);vals[n]=randomInt(GTs[n]+1,LTs[n]);break;case "<":LTs[n]=rhs;GTs[n]=Math.min(GTs[n],LTs[n]-2);vals[n]=randomInt(GTs[n]+1,LTs[n]);break;case "=":vals[n]=Number(rhs);break;default:}}}}}
attempts++;}while(!OKQ&&attempts<100);if(!OKQ){return(null);}
Name1=my.names[randomInt(0,my.names.length-1)];do{Name2=my.names[randomInt(0,my.names.length-1)];}while(Name2.name==Name1.name);console.log("Name1, Name2",Name1,Name2);let t=my.things.slice(0);for(let i=0;i0){t=tnew.slice(0);}
break;default:}}}
if(t.length==1){let Thing1=t[0];let Thing2=t[0];}else{Thing1=t[randomInt(0,t.length-1)];attempts=0;do{Thing2=t[randomInt(0,t.length-1)];attempts++;}while(Thing2==Thing1&&attempts<100);if(Thing2==Thing1){return(null);}}
console.log('Thing1',Thing1)
q.thing=Thing1
q.txtStr=qFmt(qStr,Name1,Name2,Thing1,Thing2,vals)
console.log('methodStr',methodStr)
q.methodStr=qFmt(methodStr,Name1,Name2,Thing1,Thing2,vals)
return q}
function namesGet(){let names=[{sex:"n",name:"Sam"},{sex:"n",name:"Alex"},{sex:"m",name:"Raj"},{sex:"m",name:"Emir"},{sex:"f",name:"Aisha"},{sex:"n",name:"Jìngyi"},{sex:"n",name:"Li"},{sex:"m",name:"John"},{sex:"m",name:"Henry"},{sex:"m",name:"Patrick"},{sex:"m",name:"Josh"},{sex:"m",name:"Mr Edwards"},{sex:"m",name:"the man"},{sex:"f",name:"Betty"},{sex:"f",name:"Jill"},{sex:"f",name:"Tiffany"},{sex:"f",name:"Sofia"},{sex:"f",name:"Jasmine"},{sex:"f",name:"Mrs Jones"},{sex:"f",name:"the woman"},{sex:"n",name:"the teacher"},{sex:"n",name:"Hunter"},{sex:"n",name:"Billy"},{sex:"n",name:"Angel"},{sex:"n",name:"Drew"},{sex:"n",name:"Jazz"},{sex:"n",name:"Riley"},{sex:"n",name:"Sky"},{sex:"n",name:"Toby"},{sex:"n",name:"Sage"},{sex:"n",name:"Ariel"},{sex:"n",name:"Mason"},{sex:"n",name:"Noel"},]
return names}
function thingsLoad(){let things=[];things.push({one:"apple",many:"apples",props:["eat","give","buy"]});things.push({one:"pear",many:"pears",props:["eat","give","buy"]});things.push({one:"banana",many:"bananas",props:["eat","give","buy"]});things.push({one:"orange",many:"oranges",props:["eat","give","buy"]});things.push({one:"sandwich",many:"sandwiches",props:["eat","give","buy"]});things.push({one:"sausage",many:"sausages",props:["eat","give","buy"]});things.push({one:"salad",many:"salads",props:["eat","give","buy"]});things.push({one:"chocolate",many:"chocolates",props:["eat","give","buy"]});things.push({one:"cupcake",many:"cupcakes",props:["eat","give","buy"]});things.push({one:"pie",many:"pies",props:["eat","give","buy"]});things.push({one:"drink",many:"drinks",props:["eat","give","buy"]});things.push({one:"mouse",many:"mice",props:["give","buy"]});things.push({one:"marble",many:"marbles",props:["clr","gift","give","buy"]});things.push({one:"balloon",many:"balloons",props:["clr","give","buy"]});things.push({one:"toy",many:"toys",props:["clr","give","gift","buy"]});things.push({one:"plant",many:"plants",props:["give","gift","buy"]});things.push({one:"ball",many:"balls",props:["clr","gift","give","buy"]});things.push({one:"cat",many:"cats",props:["4legs"]});things.push({one:"dog",many:"dogs",props:["4legs"]});things.push({one:"horse",many:"horses",props:["4legs"]});things.push({one:"person",many:"people",props:["2legs"]});things.push({one:"bird",many:"birds",props:["bird","2legs","buy"]});things.push({one:"seagull",many:"seagulls",props:["bird","2legs"]});things.push({one:"robin",many:"robins",props:["bird","2legs"]});things.push({one:"blackbird",many:"blackbirds",props:["bird","2legs"]});things.push({one:"pen",many:"pens",props:["clr","give","buy"]});things.push({one:"book",many:"books",props:["give","buy"]});things.push({one:"May",many:"May",props:["month"]});things.push({one:"July",many:"July",props:["month"]});things.push({one:"March",many:"March",props:["month"]});things.push({one:"dollar",many:"dollars",props:["give","money"]});things.push({one:"coin",many:"coins",props:["give","money"]});return things}
function str2Tokens(s){let a=[];while(s.length>0){let bracStt=s.indexOf("[");if(bracStt>-1){let bracEnd=s.indexOf("]",bracStt);if(bracEnd<0){return null;}
let before=s.substr(0,bracStt);let inside=s.substr(bracStt+1,bracEnd-bracStt-1);let after=s.substr(bracEnd+1,s.length-bracEnd-1);if(before.length>0)
a.push(["o",before]);if(inside.length>0)
a.push(["i",inside]);s=after;}else{if(s.length>0)
a.push(["o",s]);s="";}}
return a;}
function doLang(){let Lang="en";switch(Lang){case "en":let LangNo=0;LangMistake="Whoops, that was the wrong answer, try again";LangSuccess="Well Done!";LangSkipQ="(skip question)";LangClue="Clue";break;case "es":LangNo=1;break;default:}}
function qsFilter(){let qs=my.qs.slice(0);if(my.filter.lvl>0){let temp=[];for(let i=0;i=my.filter.lvl-1&&qs[i][QLevelAt]<=my.filter.lvl+1)
temp.push(qs[i]);}
qs=temp.slice(0);}
if(my.filter.op.length>0){let temp=[];for(i=0;i=0){s+="["+tokens[i][1]+"]";}else{parser.setVarVal("x",vals[0]);parser.setVarVal("y",vals[1]);parser.setVarVal("z",vals[2]);parser.newParse(tokens[i][1]);let val=parser.getVal();plural=val;s+=val.toString()}}}
s=toSentenceCase(s);return(s);}
function namesChg(qStr,Name1,Name2){qStr=qStr.replace(/ he /gi," %he1 ");qStr=qStr.replace(/ she /gi," %he2 ");qStr=qStr.replace(/ his /gi," %his1 ");qStr=qStr.replace(/ her /gi," %his2 ");console.log('qStr before:',qStr)
qStr=qStr.replace(/John/g,Name1.name);let sex1=Name1.sex=='n'?Math.random()>0.5?'m':'f':Name1.sex
qStr=qStr.replace(/%he1/gi,sex1=='m'?"he":"she");qStr=qStr.replace(/%his1/gi,sex1=='m'?"his":"her");qStr=qStr.replace(/Betty/g,Name2.name);let sex2=Name2.sex=='n'?Math.random()>0.5?'m':'f':Name2.sex
qStr=qStr.replace(/%he2/gi,sex2=='m'?"he":"she");qStr=qStr.replace(/%his2/gi,sex2=='m'?"his":"her");console.log('qStr after:',qStr)
return qStr}
function thingsChg(qStr,Thing1,Thing2,plural){qStr=thingChg(qStr,/Cupsx/g,Thing1,plural);qStr=thingChg(qStr,/Cupsy/g,Thing1,plural);qStr=thingChg(qStr,/Platesx/g,Thing2,plural);qStr=thingChg(qStr,/Platesy/g,Thing2,plural);qStr=thingChg(qStr,/Cups/g,Thing1,plural);qStr=thingChg(qStr,/Plates/g,Thing2,plural);return qStr;}
function thingChg(s,from,thing,plural){if(s.search(from)>-1){return s.replace(from,thingGet(thing,plural))}else{return s}}
function anssGet(q){console.log('anssGet',q)
parser.setVarVal("x",vals[0]);parser.setVarVal("y",vals[1]);parser.setVarVal("z",vals[2]);parser.newParse(q.formula);correctAnswer=parser.getVal();let answers=getWrongAnswers(correctAnswer,true,true);let anspos=randomInt(0,3);answers[anspos]=correctAnswer;if(hasThing(q)){answers=answers.map(ans=>{ans+=' '
ans+=Math.abs(ans)==1?q.thing.one:q.thing.many
return ans})}
q.anss=answers
q.ansNo=anspos}
function hasThing(q){let s=q.txt
if(s.search(/cups/i)>=0)return true
if(s.indexOf('plates')>=0)return true
return false}
function getWrongAnswers(ans,NoNegQ,NoZeroQ){var wrongs=[]
wrongs.push(ans+Math.round(Math.random()*(9-2)+2));wrongs.push(ans-Math.round(Math.random()*(9-2)+2));wrongs.push(ans+1);wrongs.push(ans+2);wrongs.push(ans+3);wrongs.push(ans-1);wrongs.push(ans-2);wrongs.push(ans-3);wrongs.push(ans+10);wrongs.push(ans-10);wrongs.push(ans+20);wrongs.push(ans*2);wrongs.push(ans*2-1);wrongs.push(ans*2+1);wrongs.push(Math.round(ans/2));var temp=[]
for(var i=0;i0){dec1=getRandomInt(0,dec-a.toString().length+1);dec2=getRandomInt(0,dec-b.toString().length+1);if(dec1>0)a*=Math.pow(10,dec1);if(dec2>0)b*=Math.pow(10,dec2);}
my.q=fmtDec(a,dec)+' + '+fmtDec(b,dec)+' =';my.tgtN=fmtDec(a+b,dec)
break;case 'sub':a=getRandomInt(min,max);b=getRandomInt(min,max);if(dec>0){let dec1=getRandomInt(0,dec-a.toString().length+1);let dec2=getRandomInt(0,dec-b.toString().length+1);if(dec1>0)a*=Math.pow(10,dec1);if(dec2>0)b*=Math.pow(10,dec2);}
if(a0){dec1=getRandomInt(0,dec);dec2=dec-dec1;decAns=dec*2;if(dec1>0)a*=Math.pow(10,dec1);if(dec2>0)b*=Math.pow(10,dec2);}
my.tgtN=fmtDec(a*b,decAns,true)
my.q=fmtDec(a,dec)+' × '+fmtDec(b,dec)+' =';break;case 'div':a=getRandomInt(min,max);b=getRandomInt(min,max);if(dec>0){dec1=getRandomInt(0,dec);dec2=dec-dec1;decAns=dec*2;if(dec1>0)a*=Math.pow(10,dec1);if(dec2>0)b*=Math.pow(10,dec2);}
my.tgtN=fmtDec(a,dec,true);my.q=fmtDec(a*b,decAns,true)+' / '+fmtDec(b,dec)+' =';break;default:}
document.getElementById('quest').innerHTML=my.q;}
function chgVal(val,doneQ){console.log("chgVal",val,val.length,my.tgtN.toString().length,doneQ);if(doneQ||(val.length==my.tgtN.toString().length)||(Number(val)==Number(my.tgtN))){choose(val);document.getElementById('user').value='';}}
function choose(val){if(!my.gameQ)return;my.resultQ=true;let gap,score
switch(my.game.typ){case 'count':val=Math.round(val);console.log("choose",my.game.typ,val,my.tgtN);g.textAlign='center';g.font='bold 65px Arial';s=''+my.tgtN;g.fillStyle='blue';fancyText(s,w/2,120);if(val==my.tgtN){g.font='bold 40px Arial';s='Exactly Right!';g.fillStyle='gold';fancyText(s,w/2,170);}
let gap=val-my.tgtN;let score=calcScore(gap/2);if(score!=0){my.score+=score;document.getElementById('score').innerHTML=my.score;g.font='36px Arial';s='Score +'+score;g.fillStyle='black';fancyText(s,w/2,250);}
break;case 'pct':val=Math.round(val);console.log("choose",my.game.typ,val,my.tgtN);g.font='bold 44px Arial';g.textAlign='center';s=my.tgtN+'%';g.fillStyle='black';fancyText(s,graphWd/2,graphHt/2+18);gap=val-my.tgtN;score=calcScore(gap);if(score!=0){my.score+=score;document.getElementById('score').innerHTML=my.score;s='Score +'+score;g.fillStyle='black';fancyText(s,w/2,250);}
break;case 'add':case 'sub':case 'mul':case 'div':document.getElementById('quest').innerHTML=my.q+' '+my.tgtN;gap=val-my.tgtN;score=calcScore(gap);if(score!=0){my.score+=score;document.getElementById('score').innerHTML=my.score;}
break;default:}
if(my.gameQ)setTimeout(newRound,1500);}
function getRegular(midX,midY,radius,sttAngle,n){let pts=[];let dAngle=Math.PI*2/n;for(let i=0;i-1){let condarr=cond.split(condtype);return([condarr[0],condtype,condarr[1]]);}else{return([]);}}
function evalExpr(expr,vals){let x=0;let no=varNo(expr);if(no>=0){x=vals[no];}else{x=Number(expr);}
return(x);}
function varNo(valName){switch(valName){case "x":return 0;case "y":return 1;case "z":return 2;default:}
return-1;}
function getRadioHTML(prompt,id,lbls,func){let s='';s+='