Finding the Mean of an array

Finding the Mean of an array
0.0 0


I have a function that I have written which takes numbers in array and finds the mean eg mean([1,2,3,4,5]) === 3 or mean([1,10]) === 5.5 but what adjustments can I make it I want to find the mean of nothing. So I would my final result to look like this mean([]) === null

function mean(arr) {
  var total = 0;
  for(var i = 0; i < arr.length; i++) {
    total += arr[i];
  return total / arr.length;
function round(number) {
  return Math.round(total * arr) / arr.length;

Any help is much appreciated.


Check the length of arr and return null if it’s 0.


Tried implementing a conditional but it’s returning NaN. How would I be able to modify this so it returs null

if (!arr.length) {
    return null;