// Add the finished solution here when you receive it.

// Sum Function
function sum(array) {
  return array.reduce(function(a, b) {
    return a + b;
  });
};

// Mean Function
function mean(array) {
  return sum(array) / array.length;
};

// Median Function
function median(array) {
  if (array.length % 2 != 0)
    var median = array[(array.length - 1) / 2];
  else
    var median = (array[array.length / 2] + array[array.length / 2 - 1]) / 2.0;
  console.log(median);
  return median;
};

// __________________________________________
// Tests:  Do not alter code below this line.


oddLengthArray  = [1, 2, 3, 4, 5, 5, 7]
evenLengthArray = [4, 4, 5, 5, 6, 6, 6, 7]


function assert(test, message, test_number) {
  if (!test) {
    console.log(test_number + "false");
    throw "ERROR: " + message;
  }
  console.log(test_number + "true");
  return true;
}

// tests for sum
assert(
  (sum instanceof Function),
  "sum should be a Function.",
  "1. "
)

assert(
  sum(oddLengthArray) === 27,
  "sum should return the sum of all elements in an array with an odd length.",
  "2. "
)

assert(
  sum(evenLengthArray) === 43,
  "sum should return the sum of all elements in an array with an even length.",
  "3. "
)

// tests for mean
assert(
  (mean instanceof Function),
  "mean should be a Function.",
  "4. "
)

assert(
  mean(oddLengthArray) === 3.857142857142857,
  "mean should return the average of all elements in an array with an odd length.",
  "5. "
)

assert(
  mean(evenLengthArray) === 5.375,
  "mean should return the average of all elements in an array with an even length.",
  "6. "
)

// tests for median
assert(
  (median instanceof Function),
  "median should be a Function.",
  "7. "
)

assert(
  median(oddLengthArray) === 4,
  "median should return the median value of all elements in an array with an odd length.",
  "8. "
)

assert(
  median(evenLengthArray) === 5.5,
  "median should return the median value of all elements in an array with an even length.",
  "9. "
)
// Add the finished solution here when you receive it.

// Initial Solution 

// Sum Function
function sum(array){
  var sum = 0;
  if (array.length % 2 != 0)
    for (var i = 0; i < array.length; i++) {
      sum += array[i];
    }
  else {
    for (var i = 0; i < array.length; i++) {
      sum += array[i];
    }
  }
  return sum;
}

// Mean Function
function mean(array){
  var sum = 0;
  if (array.length % 2 != 0)
    for (var i = 0; i < array.length; i++) {
      sum += array[i];
    }
  else {
    for (var i = 0; i < array.length; i++) {
      sum += array[i];
    }
  }
  return sum/array.length;
}

// Median Function
function median(array){
  var median = 0;
  if (array.length % 2 != 0) {
    var index = array.length / 2;
    median = array[Math.floor(index)];
    }
  else {
    nums = 0;
    nums += array[(array.length / 2) - 1];
    nums += array[(array.length / 2)];
    median = nums / 2;
    }
  return median;
}

// Refactored Solution

// Sum Function
function sum(array) {
  return array.reduce(function(a, b) {
    return a + b;
  });
};

// Mean Function
function mean(array) {
  return sum(array) / array.length;
};

// Median Function
function median(array) {
  if (array.length % 2 != 0)
    var median = array[(array.length - 1) / 2];
  else
    var median = (array[array.length / 2] + array[array.length / 2 - 1]) / 2.0;
  return median;
};

// __________________________________________
// Tests:  Do not alter code below this line.


oddLengthArray  = [1, 2, 3, 4, 5, 5, 7]
evenLengthArray = [4, 4, 5, 5, 6, 6, 6, 7]


function assert(test, message, test_number) {
  if (!test) {
    console.log(test_number + "false");
    throw "ERROR: " + message;
  }
  console.log(test_number + "true");
  return true;
}

// tests for sum
assert(
  (sum instanceof Function),
  "sum should be a Function.",
  "1. "
)

assert(
  sum(oddLengthArray) === 27,
  "sum should return the sum of all elements in an array with an odd length.",
  "2. "
)

assert(
  sum(evenLengthArray) === 43,
  "sum should return the sum of all elements in an array with an even length.",
  "3. "
)

// tests for mean
assert(
  (mean instanceof Function),
  "mean should be a Function.",
  "4. "
)

assert(
  mean(oddLengthArray) === 3.857142857142857,
  "mean should return the average of all elements in an array with an odd length.",
  "5. "
)

assert(
  mean(evenLengthArray) === 5.375,
  "mean should return the average of all elements in an array with an even length.",
  "6. "
)

// tests for median
assert(
  (median instanceof Function),
  "median should be a Function.",
  "7. "
)

assert(
  median(oddLengthArray) === 4,
  "median should return the median value of all elements in an array with an odd length.",
  "8. "
)

assert(
  median(evenLengthArray) === 5.5,
  "median should return the median value of all elements in an array with an even length.",
  "9. "
)
Complete Release 3 of Challenge 7.8 for Practice