title: Can't access global variable in while loop
body: <p>I am trying to solve this programming problem, but i am unable to access the global variable inside the while loop. It gives me undefined when i access the global variable.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function equal(h1, h2, h3) {
    let sum1 = 0;
    let sum2 = 0;
    let sum3 = 0;
    let first = [];
    let second = [];
    let third = [];
    let count = 0;
    while (h1.length !== 0) {
      var popped = h1.pop();
      sum1 += popped;
      first.push(popped);
    }
    while (h2.length !== 0) {
      var popped = h2.pop();
      sum2 += popped;
      second.push(popped);
    }
    while (h3.length !== 0) {
      var popped = h3.pop();
      sum3 += popped;
      third.push(popped);
    }
    while (sum1 === sum2 &amp;&amp; sum2 === sum3 &amp;&amp; sum3 === sum1) {
      // Below two consoles are not printing anything.
      console.log(sum1, sum2, sum3);
      console.log(h1, h2, h3);
      if (sum1 &gt; sum2) {
        var x = first.pop();
        sum1 = sum1 - x;
      } else if (sum2 &gt; sum3) {
        var y = second.pop();
        sum2 = sum2 - y;
      } else {
        var z = third.pop();
        sum3 = sum3 - z;
      }
    }
  }
  
  console.log(equal([3, 2, 1, 1, 1], [4, 3, 2], [1, 1, 4, 1]));</code></pre>
  </div>
  </div>
  </p>