I would like to check multiple arrays if their sum is equal to zero and if yes, output yes. But only the first array is being read by the code.
There should be 3 outputs of (true or false) but I'm getting only 1 output instead
const arr0 = [3];
const arr1 = [4];
const arr2 = [2, 8, -9, 1];
const arr = [arr0, arr1, arr2];
const zeroSum = arr => {
const map = new Map();
let sum = 0;
for(let i = 0; i < arr.length; i++){
sum += arr[i];
if(sum === 0 || map.get(sum)){
return true;
};
map.set(sum, i);
};
return false;
};
console.log(zeroSum(arr));
sum += arr[i].arr[i]is an array and you're trying to add it to 0. You need to loop over the elements ofarr[i]and add those to sum which would need to be declared for each outer loop.