title: Javascript. How can I access a variable outside of a function?
body: <p>I'm trying to create a simple application that generates a random question with an onclick, and will generate a solution to said question with a different onclick.  I've been able to create the random question function simple enough, but what I'm having trouble with is the solution part.  Essentially I cant access the variables produced from the "generate question" function. </p>
  
  <p>The following function generates a random question: "Convert 10mg/mL to mcg/mL" </p>
  
  <pre><code>const arr = ['mg/mL', 'mcg/mL', 'mg/L'];
  
  function genConversion () {
    function clearBox(elementID){ //this resets the question
        document.getElementById(elementID).innerHTML = "";
    }
  
    clearBox('output');
  
    function shuffle(a) { 
      for (let i = a.length - 1; i &gt; 0; i--) {
          const j = Math.floor(Math.random() * (i + 1));
          [a[i], a[j]] = [a[j], a[i]];
      }
      return a;
    }
  
     function randomNum() {
      var number = Math.floor((Math.random() * 100) +1);
      return number
    }
  
    var num = randomNum()
  
    shuffle(arr);
  
    output.innerHTML += "Convert " + num + ' ' + arr[0] + ' to ' + arr[1]
  
  }
  </code></pre>
  
  <p>Now, when I try to create a function that will solve the generated problem (the actual logic will come once I can figure out how to access the question variables), I cant seem to access variable 'num'.  Interestingly, I am able to access the random array element arr[0].</p>
  
  <pre><code>function solveProb() {
    console.log(arr[0]);
    console.log(num);
  }
  </code></pre>
  
  <p>Im sure this has something to do with scope that Im having trouble with, but any advice would be appreciated.  </p>