利用getRandom随机数,做一个猜数字的小游戏:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
//猜数字???
//无参有返回结果集
function getRandom(){
//随机数
var result=parseInt((Math.random()*100));
//
console.log(result);
return result;
}
function cai(){
var result=getRandom();
for(var i=1;i<=5;i++){
var a=parseInt(prompt("请猜数字"));
if(result==a){
alert("恭喜猜对了奖励笔记一份");
break;
}else if(result > a){
alert("猜小了,继续");
}else{
alert("猜大了,继续加油");
}
}
window.alert("最终结果是:"+result);
}
</script>
</head>
<body>
<input type="button" value="点击" onmousemove="cai()" />
</body>
</html>