I am having some difficulity in how to actually get mode and count for the biggest value of the map. I can't get to count for mode because it show for multiplication 2, what i think happens in my code, is the matter of the loop. Thanks in advance.
var data1 = [1, 1, 1, 2, 2, 4, 1, 1]
function getMode (arr) {
var obj = {};
var max = 0;
let result = []
arr.forEach(element => {
if (!obj[element]) obj[element] = 0
obj[element]++
});
if (Object.keys(obj).length === result.length) {
result = []
}
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = (obj[arr[i]] + 1) || 1;
}
for (var key in obj) {
if (obj[key] > max) {
result = [key]
max = obj[key]
} else if (obj[key] === max) {
result.push(key)
}
}
return ("most number value " + result + " with " + max)
}
console.log(getMode(data1));