-6

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.

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 && sum2 === sum3 && sum3 === sum1) {
    // Below two consoles are not printing anything.
    console.log(sum1, sum2, sum3);
    console.log(h1, h2, h3);
    if (sum1 > sum2) {
      var x = first.pop();
      sum1 = sum1 - x;
    } else if (sum2 > 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]));
Barmar
  • 596,455
  • 48
  • 393
  • 495
devesh
  • 89
  • 1
  • 8
  • Your function does not return anything so `console.log` will print "undefined". – Johnny Mopp Jun 25 '19 at 18:16
  • I meant, when i access console.log(sum1, sum2, sum3) or console.log(h1, h2, h3) indside it prints undefined. – devesh Jun 25 '19 at 18:17
  • 2
    @devesh None of those are global variables, they're all local to the function. – Barmar Jun 25 '19 at 18:18
  • You may not understand what global variables are. Here's a good [stack](https://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) to help you. – bflemi3 Jun 25 '19 at 18:21
  • But, i can access variables inside the function anywhere. right? – devesh Jun 25 '19 at 18:21
  • Please update the code you posted to show the actual problem you're having. Add the `console.log` calls that aren't working as expected. – Barmar Jun 25 '19 at 18:22
  • 1
    you're never getting to those `console.log` lines because the `while` condition is not true. – Barmar Jun 25 '19 at 18:51
  • 1
    `sum1 == 8, sum2 == 9, sum3 == 7`, so you never go into the `while` loop at all. – Barmar Jun 25 '19 at 18:53
  • @devesh Please don't forget to accept an answer by checking the checkmark next to each answer. – bflemi3 Aug 16 '19 at 18:14

2 Answers2

1

You are not returning anything from your method equal(). So if you try to console it, it will display undefined. Please try returning some value.


Code Snippet Attached (Edited)

function equal(h1, h2, h3){
    let sum3 = 0;
    let third = [];
    let count = 0;
    while (h3.length !== 0){
        var popped = h3.pop();
        sum3 += popped;
        third.push(popped);
        //here are the global variables inside while loop
        console.log(sum3)
    }
}

console.log(equal([ 3, 2, 1, 1, 1 ], [ 4, 3, 2 ], [ 1, 1, 4, 1 ]));
Faraz Javed
  • 139
  • 6
  • This is true, but what does it have to do with accessing global variables in the `while` loop? – Barmar Jun 25 '19 at 18:19
  • His comment says **when i access console.log(sum1, sum2, sum3) or console.log(h1, h2, h3) indside it prints undefined** – Barmar Jun 25 '19 at 18:21
  • @Barmar we can access global variable inside while loop, I have updated my answer, in the code snippet, you can see the global variable being printed. – Faraz Javed Jun 25 '19 at 18:33
1

Some Definitions

Global Variables and Scope

Lexical Scope


Some Misunderstandings

1. Misunderstanding what a global variable is.

Variables are accessible within a function's lexical scope and evaluated in order, from local scope, to parent scope, to global scope (window in a browser, global in nodejs). What you're referring to as "global scope" is actually the local scope of the equal function or a while loop.

2. Expecting a value to be returned from equal without a return statement

console.log(equal([3, 2, 1, 1, 1], [4, 3, 2], [1, 1, 4, 1]));

equal doesn't return anything, so nothing will be logged to the console.

3. Misunderstanding how JavaScript is evaluated

JavaScript evaluates code synchronously (read this for more information). So, your while loops are evaluated in sequence, one a time, until the length of each array they're evaluating becomes 0. Then your final while loop will be evaluated.

while(sum1 === sum2 && sum2 === sum3 && sum3 === sum1)

This loop will only ever iterate if all 3 conditions are true. Invoking equal as you did:

equal([3, 2, 1, 1, 1], [4, 3, 2], [1, 1, 4, 1])

will result in the final while loop being false (this is pseudo code):

sum1 = (3 + 2 + 1 + 1 + 1) = 8 
sum2 = (4 + 3 + 2) = 9 
sum3 = (1 + 1 + 4 + 1) = 7
---
sum1 != sum2 && sum2 != sum3 && sum3 != sum1

So the iteration won't occur and console.log(sum1, sum2, sum3), inside of your final while loop will not be printed.

bflemi3
  • 6,311
  • 19
  • 75
  • 144