I am a beginner in JavaScript and just wanted to get my code reviewed. Here I have created a function that takes in an array and returns the mode. If there is no more then I just want the function to return the first number. It seems a little too long and I feel like there may be more clever ways to do it. Again, keep in mind that I am a beginner, so I don't have a huge toolbox.
var mode = function(arr){
var numMapping = {};
for(var i = 0; i < arr.length; i++){
if(numMapping[arr[i]] === undefined){
numMapping[arr[i]] = 0;
}
numMapping[arr[i]] += 1;
}
var greatestFreq = 0;
var mode;
for(var prop in numMapping){
if(numMapping[prop] > greatestFreq){
greatestFreq = numMapping[prop];
mode = prop;
}
}
return parseInt(mode);
}