<!DOCTYPE html>
<html lang="zh-Hant-TW">
<head>
<title>Learing Javascript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
</style>
</head>
<body>
<P>終極密碼:請猜0~3的整數</P>
<input id="myInput" type="text" name="">
<button id="myButton">提交</button>
<script type="text/javascript">
// var x = 2;
// console.log(Math.pow(x , 3)); // pow運算 2的3次方
document.getElementById("myButton").onclick = function(){
var x = Math.random(); // random產生隨機數0~1之間,不包含1
x = x * 4; // 須包含3,所以要多1個倍數
x = Math.floor(x); //將運算的值利用floor運算為取整數
console.log(x);
if ( x == document.getElementById("myInput").value) {
alert("恭喜您猜對了");
} else {
alert("您猜錯了! 答案是" + x );
}
}
</script>
</body>
</html>
留言列表