-4

I have started studying Javascript one month ago and I have a doubt. 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. So my question is: how to give priority to the if(num==random)...?

---EDIT---

Full code:

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

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. Hope it helps.

ajmnz
  • 742
  • 7
  • 19

1 Answers1

2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Oh, sorry for that. I didn't know how to name it. But, what if I have like lots of "if(num==random)" and lots of "if(attempts==0)"? – ajmnz Feb 11 '19 at 19:07
  • ["elseif" syntax in javascript](https://stackoverflow.com/questions/4005614/elseif-syntax-in-javascript) – Nino Filiu Feb 11 '19 at 19:09
  • @ArnauJiménez — Then you probably just want a lot of `else`s too. – Quentin Feb 11 '19 at 19:09
  • But there are multiple attempts, not just one. If I put an else and the player fails it will say that he/she has lost. – ajmnz Feb 11 '19 at 19:21
  • @ArnauJiménez — I didn't say to remove the `if(attempts==0)` when you added the `else`. – Quentin Feb 11 '19 at 19:26
  • @Quentin - So what you told me was to add a blank else? – ajmnz Feb 11 '19 at 19:27
  • @ArnauJiménez — No. For it to be blank you would have to remove `if(attempts==0)` (which I just explicitly said you shouldn't do) **and** `documentGetElementById("message").innerHTML="You have lost."`, which I have also said not to do. – Quentin Feb 11 '19 at 19:28
  • @Quentin - Well, so how should it look like? I'm new to JS so a little help would be appreciated :) – ajmnz Feb 11 '19 at 19:30
  • You just add `else ` before the `if` – Quentin Feb 11 '19 at 19:34
  • Before all the `if`...? – ajmnz Feb 11 '19 at 19:45
  • No. Before the `if` that you don't want to run if the answer is correct. – Quentin Feb 11 '19 at 19:54
  • @Quentin - Thanks for your patience. That worked. I have added the full code btw. – ajmnz Feb 11 '19 at 19:57