除了hilow功能之外,我已经使程序的每个方面都可以工作。
我遇到的麻烦是让hilow()函数对程序执行任何操作。当我运行这些程序时,我可以使所有其他方面正常工作。程序所需的部分位于hilow函数内的注释中。感谢您的时间。
本质上需要发生的是,每当我按下较高的按钮时,lowerBound的数字应更改为计算机所猜测的值,这样计算机就可以将我所想的数字归零。
JS:
function getFieldValue(target){
var elm = document.getElementById(target);
var val;
if(val != REMAINING && val >= 0){
val = parseInt(elm.value);
}
else{
val = elm.value;
}
return val;
}
function getCompGuess(){
var highest = parseInt(document.getElementById("upperBound").value);
var lower = parseInt(document.getElementById("lowerBound").value);
var combine = (highest + lower)/2;
document.getElementById("currentGuess").value = combine;
}
function play(){
var remaining = getFieldValue("maxGuesses");
var compGuess = getCompGuess();
var compElm = document.getElementById("currentGuess");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
if(remaining > 0){
remaining -= 1;
}
else{
remaining = "Out of guesses";
}
document.getElementById("REMAINING").value = remaining;
}
function correct(){
var whatsLeft;
whatsLeft = document.getElementById("REMAINING").value;
if(whatsLeft >= 0){
alert("YOU WON!!!");
}
else{
alert("YOU LOSE");
}
}
function hilow(userChoice){
if(userChoice === "HIGHER"){
//get the field value from the button chosen
//if the computer guess value in the box is higher that field values
//just obtained
//take the computer guess in the guess box and put it in the lower
//value box (use document.getElementById)
var imLower = parseInt(document.getElementById("lowerBound").value);
document.getElementById("currentGuess").value = imLower;
}
else{
//get the field value from the upper limit box
//if the computer guess value in the box is lower than the field
//value just obtained
//take the computer guess in the guess box and put it in the lower
//value box (use document.getElementById)
var imHigher = parseInt(document.getElementById("lowerBound").value);
document.getElementById("currentGuess").value = imHigher;
}
play();
}
和HTML
<html>
<head>
<meta charset="UTF-8">
<script src="lab1script.js"></script>
</head>
<body>
Upper Bound: <input type="text" id="upperBound" name="upperBound" required>
<br>
Lower Bound: <input type="text" id="lowerBound" name="lowerBound" required>
<br>
Max Guesses: <input type="text" id="maxGuesses" name="maxGuesses" > <br>
Guesses Left: <input type="text" id="REMAINING" name="REMAINING"> <br>
Current Guess: <input type="text" id="currentGuess" name="currentGuess">
<br>
<input type="button" name="Start" value="Start" onclick="play()"><br>
<br>
<input type="button" name="HIGHER" value="HIGHER" onclick="hilow()">
<input type="button" name="LOWER" value="LOWER" onclick="hilow()">
<input type="button" name="CORRECT" value="CORRECT" onclick="correct()">
</body>
</html>
最佳答案
在html上进行了修复,以便调用hilow()
函数并发送<button>
名称。
事实上,我只是添加了下一行来更改您想要更改的范围
document.getElementById("upperBound").value = document.getElementById("currentGuess").value;
线条丑陋,但这将获得期望值并将其分配给相应的边界。
希望这就是您想要的。如果需要,很高兴为您解释或提供更好的解决方案。
function getFieldValue(target) {
var elm = document.getElementById(target);
var val;
if (val != REMAINING && val >= 0) {
val = parseInt(elm.value);
} else {
val = elm.value;
}
return val;
}
function getCompGuess() {
var highest = parseInt(document.getElementById("upperBound").value);
var lower = parseInt(document.getElementById("lowerBound").value);
var combine = (highest + lower) / 2;
document.getElementById("currentGuess").value = combine;
}
function play() {
getRemaining();
var compGuess = getCompGuess();
var compElm = document.getElementById("currentGuess");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
}
function correct() {
var whatsLeft;
whatsLeft = document.getElementById("REMAINING").value;
if (whatsLeft >= 0) {
alert("YOU WON!!!");
} else {
alert("YOU LOSE");
}
}
function hilow(userChoice) {
if (userChoice === "HIGHER") {
//get the field value from the button chosen
//if the computer guess value in the box is higher that field values
//just obtained
//take the computer guess in the guess box and put it in the lower value
//box(use document.getElementById);
document.getElementById("lowerBound").value = document.getElementById("currentGuess").value;
} else {
//get the field value from the upper limit box
//if the computer guess value in the box is lower than the field value
//just obtained
//take the computer guess in the guess box and put it in the lower value
// box(use document.getElementById);
document.getElementById("upperBound").value = document.getElementById("currentGuess").value;
}
play();
}
var first = true;
function getRemaining() {
if (first) {
var max = getFieldValue("maxGuesses");
first = false;
document.getElementById("REMAINING").value = max - 1;
} else {
var remaining = getFieldValue("REMAINING");
if (remaining > 0) {
remaining -= 1;
} else {
remaining = "Out of guesses";
}
document.getElementById("REMAINING").value = remaining;
}
}
<html>
<head>
<meta charset="UTF-8">
<script src="lab1script.js"></script>
</head>
<body>
Upper Bound: <input type="text" id="upperBound" name="upperBound" required>
<br> Lower Bound: <input type="text" id="lowerBound" name="lowerBound" required>
<br> Max Guesses: <input type="text" id="maxGuesses" name="maxGuesses"> <br> Guesses Left: <input type="text" id="REMAINING" name="REMAINING"> <br> Current Guess: <input type="text" id="currentGuess" name="currentGuess">
<br>
<input type="button" name="Start" value="Start" onclick="play()"><br>
<br>
<input type="button" name="HIGHER" value="HIGHER" onclick="hilow(name)">
<input type="button" name="LOWER" value="LOWER" onclick="hilow(name)">
<input type="button" name="CORRECT" value="CORRECT" onclick="correct()">
</body>
</html>