(function(){
var?div=document.createElement("div")
,create=document.createElement("input")
,tm=document.createElement("div")
,score=document.createElement("div")
,exp=[{js:"+",w:"+"},{js:"-",w:"-"},{js:"*",w:"×"},{js:"/",w:"÷"}];
//出題
function?ct(){
score.innerHTML="開始做題吧";
score.style.color="#000";
tm.innerHTML="";
tm.list=[];
for(var?i=0;i<10;i++){
var?s=document.createElement("span")
,p=document.createElement("input")
,jg=document.createElement("span")
,rnd=parseInt(Math.random()*4)
,v1=parseInt(Math.random()*1000)//千以內
,v2=exp[rnd]
,v3=parseInt(Math.random()*1000);
//除法,規範數值
if(rnd==3){
while(v3==0){
v3=parseInt(Math.random()*1000);
};
//尋找整除
if(v1<v3){
var?t=v3;
v3=v1;
v1=t;
};
if(Math.random()*10>5?&&?v1/v3<v3){//壹半幾率調整除數與被除數的間隔
v3=parseInt(v3/3)+1;
};
if(v1%v3!=0){
v1-=v1%v3;
};
};
s.innerText=v1+v2.w+v3+"=";
p.type="text";
p.style.width="80px";
p._value=eval(v1+v2.js+v3)+"";
p.jg=jg;
p.onblur=js;
tm.appendChild(s);
tm.appendChild(p);
tm.appendChild(jg);
tm.appendChild(document.createElement("br"));
tm.list.push(p);
}
}
//出題按鈕
create.type="button";
create.value="出題";
create.onclick=ct;
//顯示界面
div.appendChild(create);
div.appendChild(tm);
div.appendChild(score);
document.body.appendChild(div);
//顯示題目
ct();
//計算結果
function?js(e){
var?o=e.target;
//答案錯誤提示部分
if(o.value==o._value){
o.jg.innerHTML="對";
o.jg.style.color="blue";
}else?if(o.value!=""){
o.jg.innerHTML="錯";
o.jg.style.color="red";
}else{
o.jg.innerHTML="";
};
var?count=0,ecount=0,wzcount=0;
for(var?i=0;i<tm.list.length;i++){
o=tm.list[i];
if(o.value==o._value){
count++;
}else?if(o.value!=""){
ecount++;
}else{
wzcount++;
};
};
score.style.color="#000";
if(wzcount==0){
if(count==10){
score.innerHTML="滿分100";
score.style.color="blue";
}else?if(count>5){
score.innerHTML=count*10+"分,加油,改正小錯誤";
}else{
score.innerHTML=count*10+"分,好挫";
score.style.color="red";
};
for(var?i=0;i<tm.list.length;i++){
o=tm.list[i];
o.jg.innerHTML=o._value;
};
}else?if(wzcount<4){
score.innerHTML="沖啊";
}else?if(wzcount<6){
score.innerHTML="快點,就好了";
}else{
score.innerHTML="沈著冷靜";
};
};
})()