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]));