我目前正在为每个语句使用(#userGuess")。val()但希望有一个名为userGuess的变量来拉取#userGuess的输入值,以便我可以用它进行比较。这个项目的目标是创建一个应用程序,根据随机生成的数字,告诉您是猜测是热还是冷。这是我到目前为止: https://jsfiddle.net/jelane20/pLnch91t/
var randomNumber = 0;
var guessCount = 0;
var userGuess = 0;
var win = false;
//user guess
function compareGuess(){
if ($("#userGuess").val() == randomNumber) {
changeFeedback('You Win!');
win = true;
} else if ($("#userGuess").val() > randomNumber+50){
changeFeedback('You are very cold');
} else if ($("#userGuess").val() < randomNumber+50){
changeFeedback('You are very cold');
} else if ($("#userGuess").val() > randomNumber+30 && $("#userGuess").val() <= randomNumber+50){
changeFeedback('You are cold');
} else if ($("#userGuess").val() < randomNumber+30 && $("#userGuess").val() >= randomNumber+50){
changeFeedback('You are cold');
} else if ($("#userGuess").val() > randomNumber+20 && $("#userGuess").val() <= randomNumber+30){
changeFeedback('You are warm');
} else if ($("#userGuess").val() < randomNumber+20 && $("#userGuess").val() >= randomNumber+30){
changeFeedback('You are warm');
} else if ($("#userGuess").val() > randomNumber+10 && $("#userGuess").val() <= randomNumber+20){
changeFeedback('You are hot');
} else if ($("#userGuess").val() < randomNumber+10 && $("#userGuess").val() >= randomNumber+20){
changeFeedback('You are hot');
} else if ($("#userGuess").val() >= randomNumber+1 && $("#userGuess").val() <= randomNumber+10){
changeFeedback('You are very hot');
} else if ($("#userGuess").val() <= randomNumber+1 && $("#userGuess").val() >= randomNumber+10){
changeFeedback('You are very hot');
}
}
<section class="game"> <!-- Guessing Section -->
<h2 id="feedback">Make your Guess!</h2>
<form>
<input type="text" name="userGuess" id="userGuess" class="text" maxlength="3" autocomplete="off" placeholder="Enter your Guess" required/>
<input type="submit" id="guessButton" class="button" name="submit" value="Guess"/>
</form>
<p>Guess #<span id="count">0</span>!</p>
<ul id="guessList" class="guessBox clearfix">
</ul>
</section>
答案 0 :(得分:0)
这会为你做吗?
var randomNumber = 0;
var guessCount = 0;
var userGuess = 0;
var win = false;
//user guess
function compareGuess(){
userGuess = $("#userGuess").val();
if (userGuess == randomNumber) {
changeFeedback('You Win!');
win = true;
} else if (userGuess > randomNumber+50){
changeFeedback('You are very cold');
} else if (userGuess < randomNumber+50){
changeFeedback('You are very cold');
} else if (userGuess > randomNumber+30 && userGuess <= randomNumber+50){
changeFeedback('You are cold');
} else if (userGuess < randomNumber+30 && userGuess >= randomNumber+50){
changeFeedback('You are cold');
} else if (userGuess > randomNumber+20 && userGuess <= randomNumber+30){
changeFeedback('You are warm');
} else if (userGuess < randomNumber+20 && userGuess >= randomNumber+30){
changeFeedback('You are warm');
} else if (userGuess > randomNumber+10 && userGuess <= randomNumber+20){
changeFeedback('You are hot');
} else if (userGuess < randomNumber+10 && userGuess >= randomNumber+20){
changeFeedback('You are hot');
} else if (userGuess >= randomNumber+1 && userGuess <= randomNumber+10){
changeFeedback('You are very hot');
} else if (userGuess <= randomNumber+1 && userGuess >= randomNumber+10){
changeFeedback('You are very hot');
}
}
<section class="game"> <!-- Guessing Section -->
<h2 id="feedback">Make your Guess!</h2>
<form>
<input type="text" name="userGuess" id="userGuess" class="text" maxlength="3" autocomplete="off" placeholder="Enter your Guess" required/>
<input type="submit" id="guessButton" class="button" name="submit" value="Guess"/>
</form>
<p>Guess #<span id="count">0</span>!</p>
<ul id="guessList" class="guessBox clearfix">
</ul>
</section>
答案 1 :(得分:0)
好的,你并没有真正要求这个,但代码效率非常低。
这个怎么样:
var difference = $("#userGuess").val() - randomNumber;
if(difference < 0)
difference = -difference; // <- this will already safe half of the comparisons
if(difference == 0) { /*...*/ }
else if(difference > 50) { /*...*/ }
else if(difference > 30) { /*...*/ }
else if(difference > 20) { /*...*/ }
else if(difference > 10) { /*...*/ }
else { /*...*/ }
您可以省略上限比较(&lt; =),因为您已经在之前的if中检查了! &gt; = 1比较已经过时,因为是唯一的情况。
你要求的任务,顺便说一句。你间接地得到了差异(但我首先减去了随机数)。