js 猜数字游戏
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function makenum(){
var a1=document.getElementById("a");
var t=parseInt(Math.random()*8 + 1);
a1.value=t;
initCount();
var start=Date;
alert(start);
}
function initCount(){
count=0;
}
function check(){
var num=document.getElementById("a").value;
var num1=document.getElementById("c").value;
var check="0123456789";
for(var i=0;i<num1.length;i++){
var ran=num1.charAt(i);
if(check.lastIndexOf(ran)<0){
alert('请输入数字');
return false;
}
}
if(num1>num){
alert('你猜的大了');
count++;
}else if(num1<num){
alert('你猜的小了');
count++;
}else if(num1==num){
count++;
alert('你猜了'+count+'次,终于猜对了');
initCount();
}
}
</script>
<body>
<input type="password" id="a"/>
<input type="button" value="生成随机数字" onclick="makenum()"/><br>
<input type="text" id="c"/>
<input type="button" " value="看看对了没有" onclick="check()"/>
</body>
</html>