gpt4 book ai didi

javascript - 确定 Number 是 Higher 还是 Lower 不起作用的函数

转载 作者:行者123 更新时间:2023-11-29 17:46:28 25 4
gpt4 key购买 nike

除 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>

关于javascript - 确定 Number 是 Higher 还是 Lower 不起作用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com