As a beginner coder, I am working on a code that should return 1 if the mean is equal to the mode and 0 otherwise. However, my current code only outputs 0 even when it should be returning 1. Any guidance or assistance in identifying where I may have made an error would be highly appreciated!
function ArrayChallenge(arr) {
let sum = 0;
let totalNum = arr.length;
let mean = sum / totalNum;
for (i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
sum += arr[i];
}
}
const numList = {};
arr.forEach(elem => numList[elem] = numList[elem] + 1 || 1);
let mode = [];
let numMax = 0;
for (const key in numList) {
if(numList[key] > numMax) {
mode = [Number(key)];
numMax = numList[key];
} else if (numList[key] === numMax) {
mode.push(Number(key));
}
}
if (mode.length === Object.keys(numList).length) mode = [];
if (mean === mode) {
return 1;
} else return 0;
}