一个人机比较大小的JS小程序


//写一个人机比大小(1~10)的网页小程序。
<html>
<head>
<script language="javascript" type="text/javascript">
var i=0;
var a=0;
var num=10;
function test(){
var number = Math.floor(Math.random()*num) + 1;
var val = document.theform.thetext.value;
i++;
if(val > number){
a++;
//alert(a);
}
else if(val < number){
a--;
//alert(a);
}

document.getElementById("aa").innerHTML = "<h1>第" + i + "次决斗:";
if(val > number) document.getElementById("aa").innerHTML += "电脑出牌 :" + number + " < " + val;
else if(val < number) document.getElementById("aa").innerHTML += "电脑出牌 :" + number + " > " + val;
else document.getElementById("aa").innerHTML += "电脑出牌 :" + number + " = " + val;
document.getElementById("aa").innerHTML += "</h1><br/>";

if(i==3){
document.getElementById("aa").innerHTML += "<h1>";
if(a>0){
document.getElementById("aa").innerHTML += "<h1>YOU WIN!</h1>";
}
else if(a<0){
document.getElementById("aa").innerHTML += "<h1>YOU LOST!</h1>";
}
else document.getElementById("aa").innerHTML += "OH MY GOD!!!";
document.getElementById("aa").innerHTML += "<h1>";
i=0;//重新开始
}
}
</script>
</head>

<body>
<center>
<h1>You have tree times!!!</h1>
<form name="theform">
<input type="text" name="thetext" />
<input type="button" name="butt" value="确定" οnclick="test()"/>
</form>
<div id="aa">
</div>
</center>
</body>
</html>