以下代码用于猜测数字游戏"在一个网页中,用户是想到数字的人,而计算机则是猜测用户正在思考的数字。所以我需要知道如何自动减少" Guesses Remaining"中的值(数字)。输入字段。
<html>
<head>
<meta charset="UTF-8">
<script src="lab1a.js"></script>
</head>
<body>
Guess a number game: <br/> <br/>
Upper Bound: <input id="UPPER" type="text"></input> <br/>
Lower Bound: <input id="LOWER" type="text"></input> <br/>
Guesses Remaining: <input id="REMAINING" type="text"></input> <br/>
<button onclick="play()">Play!</button> <br/>
Computer's Guess: <span id="COMP_GUESS"></span> <br/>
<button onclick='high("GUESS_HIGHER")'>Guess Higher</button>
<button onclick="low('GUESS_LOWER')">Guess Lower</button>
<button disabled=true onclick="correct()" id="CORRECT">Correct!!!</button>
</body>
</html>
以下是javascript代码:
function getFieldValue(target) {
var elm = document.getElementById(target);
var val = parseInt(elm.value);
return val;
}
function getCompGuess() {
var upper = getFieldValue("UPPER");
var lower = getFieldValue("LOWER");
return (parseInt((upper + lower) / 2))
}
/* User starts game. */
function play() {
document.getElementById('CORRECT').removeAttribute("disabled");
var remaining = getFieldValue("REMAINING");
var compGuess = getCompGuess();
var compElm = document.getElementById("COMP_GUESS");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
}
/* "Correct" Button pressed. */
function correct() {
var r = getFieldValue("REMAINING");
alert("Computer wins with " + r + " guesses remaining.");
}
/* Higher or lower button */
function high(userChoice) {
if (userChoice === "GUESS_HIGHER")
var low = getFieldValue("LOWER");
if (document.compGuess > low) {
document.getElementById("LOWER").value =
document.compGuess;
}
play();
}
function low(userChoice) {
if (userChoice === "GUESS_LOWER")
var high = getFieldValue("UPPER");
if (document.compGuess < high) {
document.getElementById("UPPER").value = document.compGuess;
}
play();
}
答案 0 :(得分:0)
这是您的问题的答案“所以我需要知道如何自动减少”Guesses Remaining“输入字段中的值(数字)。”
function getFieldValue(target) {
var elm = document.getElementById(target).value;
var val = parseInt(elm);
return val;
}
function getCompGuess() {
var upper = getFieldValue("UPPER");
var lower = getFieldValue("LOWER");
return (parseInt((upper + lower) / 2))
}
/* User starts game. */
function play() {
var guess_remaining = document.getElementById("REMAINING").value;
document.getElementById('CORRECT').removeAttribute("disabled");
var remaining = getFieldValue("REMAINING");
if(isNaN(remaining)){ //When you first call getFieldValue("REMAINING") it returns NaN, means user just started to play
var guess_remaining = 5;
document.getElementById("REMAINING").value = guess_remaining;
}
else {
if(guess_remaining>0) {
guess_remaining = guess_remaining-1;
var compGuess = getCompGuess();
var compElm = document.getElementById("COMP_GUESS");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
}
document.getElementById("REMAINING").value = guess_remaining;
}
}
/* "Correct" Button pressed. */
function correct() {
var r = getFieldValue("REMAINING");
alert("Computer wins with " + r + " guesses remaining.");
}
/* Higher or lower button */
function high(userChoice) {
if (userChoice === "GUESS_HIGHER")
var low = getFieldValue("LOWER");
if (document.compGuess > low) {
document.getElementById("LOWER").value =
document.compGuess;
}
play();
}
function low(userChoice) {
if (userChoice === "GUESS_LOWER")
var high = getFieldValue("UPPER");
if (document.compGuess < high) {
document.getElementById("UPPER").value = document.compGuess;
}
play();
}
Guess a number game: <br/> <br/>
Upper Bound: <input id="UPPER" type="text"></input> <br/>
Lower Bound: <input id="LOWER" type="text"></input> <br/>
Guesses Remaining: <input id="REMAINING" type="text"></input> <br/>
<button onclick="play()">Play!</button> <br/>
Computer's Guess: <span id="COMP_GUESS"></span> <br/>
<button onclick='high("GUESS_HIGHER")'>Guess Higher</button>
<button onclick="low('GUESS_LOWER')">Guess Lower</button>
<button disabled=true onclick="correct()" id="CORRECT">Correct!!!</button>