title: Struggling with where to declare a variable in a very basic Javascript game
body: <p>I've been doing a basic Pluralsight Javascript course at the end of which I need to make a simple text based game using:</p>
  
  <pre><code>confirm()
  prompt()
  alert()
  If/else
  math.round()
  math.random()
  Operators like === etc.
  </code></pre>
  
  <p>I've come up with a scenario (this is what I'm trying to achieve) in which the player is confronted by the wolf, and via a prompt() has the choice to <strong>chat</strong>, <strong>attack</strong> or <strong>run</strong>, and a rounded random number is generated in the background.  Depending on what is returned in the prompt, the below will run:</p>
  
  <p>a) If the response is <strong>run</strong>, then a confirm() window popups up asking if the player is sure they want to do that.</p>
  
  <ul>
  <li>If the value returned is true and the random number equals 0, then the player is alerted he gets away (survives).</li>
  <li>If the value returned is true but the random number equals q, the player gets alerted that he dies (eaten by the wolf).
  
  <ul>
  <li>If the value is false, the player is alerted that he dies.</li>
  </ul></li>
  </ul>
  
  <p>b) If the response to the prompt is <strong>attack</strong> then the player is alerted he survives.</p>
  
  <p>c) If the response is <strong>chat</strong>, the player is alerted that he dies.</p>
  
  <p>d) if the response is <strong>something else</strong>, he is alerted that he dies.</p>
  
  <p>My issue is that how to structure the code.  I understand it is good practice to declare all of my variables at the beginning of my script.  But how do I declare this:</p>
  
  <pre><code>var response = confirm ("Are you sure you want to do that? It's risky");
  </code></pre>
  
  <p>but not run it at that time (I only want to run it if the player returns 'run').</p>
  
  <p>My question may be more clear you look at my code post my code below. Please help! </p>
  
  <pre><code>alert("You're off to see Grandma and you're wandering through a deep dark  forest. Suddenly you feel hot breath on the back of your neck and smell the unmistakeable stench of canine.  You turn around - it's the wolf!");  
  var choice = prompt("His piercing red eyes stare through you.  He steps closer.  What do you do - chat, attack or run?");
  var randomNumber = Math.round(Math.random());
  //The line below I only want to run if "run" is the value returned on the prompt() above.
  var response = confirm ("Are you sure you want to do that? It's risky"); 
  
  if (choice === run)
      if (response === true &amp;&amp; randomNumber === 0){
      alert("Wow - you made it to Grandma's.  Well done - you're safe!");
      }
      else if (response === true &amp;&amp; randomNumber === 1){
      alert("Oh - too bad!  You couldn't out run the wolf - he catches you and eats you!")
      }
      else{
      alert("Oh no - indecisive! While you're standing there trying to choose what to do, the wolf pounces and - BAM!  You're dinner.");  
      }}
  
  else if (choice === attack){
      alert("Brave move - you hit the wolf over the head with a stick and run to Grandma's. You're out of trouble!")
      }
  
  else if (choice === chat){
      alert("The wolf invites you in for a cup of tea and some biscuits.  After a nice chat about the weather, he eat you.  Bad move.")
      }
  
  else {
      alert("You choose to" + " " + choice + "?" + " " + "- what fairytale are you in? Weird.  The wolf eats you.  You're dead.")
      }
  </code></pre>