I'm facing a challenge while working on this coding problem. It seems that I can't access the global variable within the while loop, as it returns undefined whenever I try to do so.
function calculateSum(arr1, arr2, arr3) {
let sum1 = 0;
let sum2 = 0;
let sum3 = 0;
let first = [];
let second = [];
let third = [];
while (arr1.length !== 0) {
var poppedItem = arr1.pop();
sum1 += poppedItem;
first.push(poppedItem);
}
while (arr2.length !== 0) {
var poppedItem = arr2.pop();
sum2 += poppedItem;
second.push(poppedItem);
}
while (arr3.length !== 0) {
var poppedItem = arr3.pop();
sum3 += poppedItem;
third.push(poppedItem);
}
while (sum1 === sum2 && sum2 === sum3 && sum3 === sum1) {
// The following two console logs are not functioning correctly.
console.log(sum1, sum2, sum3);
console.log(arr1, arr2, arr3);
if (sum1 > sum2) {
var x = first.pop();
sum1 -= x;
} else if (sum2 > sum3) {
var y = second.pop();
sum2 -= y;
} else {
var z = third.pop();
sum3 -= z;
}
}
}
console.log(calculateSum([3, 2, 1, 1, 1], [4, 3, 2], [1, 1, 4, 1]));