简体   繁体   English

Javascript 仅在另一个“if”失败时执行“if”

[英]Javascript only execute an "if" if the other "if" fails

I have started studying Javascript one month ago and I have a doubt.我一个月前开始学习 Javascript 并且我有一个疑问。 I'm programming a simple game that thinks of a random number and you have to guess it before you run out of attempts.我正在编写一个简单的游戏,它会考虑一个随机数,您必须在尝试用完之前猜测它。 To determine if the player guessed right or wrong the number, I have done the following (reduced code):为了确定玩家是否猜对了数字,我做了以下(简化代码):

start(){
num=documentGetElementById("num").value;
attempts=10;
}
try(){
attempts=attempts-1;
if(num==random)documentGetElementById("message").innerHTML="You have won.";
if(attempts==0)documentGetElementById("message").innerHTML="You have lost.";
}

What happens here is that when you guess the number in the last attempt (1 attempt left) it says you have lost.这里发生的情况是,当您在最后一次尝试(还剩 1 次尝试)中猜出数字时,它说您输了。 So my question is: how to give priority to the if(num==random)...?所以我的问题是:如何优先考虑 if(num==random)...?

---EDIT--- - -编辑 - -

Full code:完整代码:

-HTML: -HTML:

<div id="game">
  <div id="message"></div>
  <div id="status"></div>
  <div id="think"></div>
  <div id="start">
    <button id="start-button" class="boto-inici" onClick="start()"><span>START</span></button>
  </div>
  <div id="try">
    <label for="tryinput">Number:</label>
    <input type="text" id="tryinput"/>
    <button class="try-button" onClick="try()"><span>TRY</span></button>
  </div>
</div>

-JS: -JS:

function start(){
  document.getElementById("start").style.display="none";
  document.getElementById("try").style.display="block";
  document.getElementById("message").style.display="block";
  document.getElementById("status").style.display="block";
  document.getElementById("message").innerHTML="I've thought a number between 1 and 100";
  document.getElementById("status").innerHTML="You have 10 attempts.";
  random = Math.floor(Math.random()*100);
  attempts=10;
}

function try(){
  attempts=attempts-1;
  document.getElementById("status").innerHTML="You have " + attempts + " attempts left.";
  var num=document.getElementById("tryinput").value;
  if(num>random)document.getElementById("message").innerHTML="The number you chose is bigger.";
  if(num<random)document.getElementById("message").innerHTML="The number you chose is smaller.";
  if(num==random){
    document.getElementById("message").innerHTML="You won.";
    document.getElementById("status").innerHTML="The number I thought was the " + random + " .";
    document.getElementById("try").style.display="none";
    document.getElementById("start").style.display="block";
  }
  if(attempts==0){
    document.getElementById("try").style.display="none";
    document.getElementById("message").innerHTML="You lost.";
    document.getElementById("status").innerHTML="The number I thought was the " + random + " .";
    document.getElementById("start").style.display="block";
  }
  if(isNaN(num)){
    alert("Please write a number.");
  }
}

It may contain some errors with the id's because I translated it.它可能包含一些 id 错误,因为我翻译了它。 Hope it helps.希望能帮助到你。

This isn't a matter of priority .这不是优先事项 Both tests are running, in the order given.两个测试都按照给定的顺序运行。

Your problem is that you don't want to do the second test at all if the first one passes.您的问题是,如果第一个测试通过,您根本不想进行第二个测试。

Use an else so the second test only runs if the first fails.使用else以便第二个测试仅在第一个失败时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM