1~100之间猜数字游戏,猜错给出相应提示,猜对给出所用次数。
源码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>which</title>
</head>
<body>
<script type="text/javascript ">
var getNum,inpNum,camPare,count=0;
getNum=Math.floor((Math.random()*100)+1);
console.log('随机数字为',getNum);
//alert(getNum );
function findIt() {
inpNum = prompt("请输入1-100之间的数字");
count++;
camPare = (inpNum - getNum);
if (isNaN(inpNum)) {
alert("error,try it again");
findIt();
}
else {
if (camPare > 0) {
alert("it's larger,try it again");
findIt();
}
else if (camPare < 0) {
alert("it's smaller,try it again");
findIt();
}
else {
alert("right,you've tried "+count+" times");
}
}
return;
}
findIt ();
</script>
</body>
</html>