Skip to content

Instantly share code, notes, and snippets.

@macikokoro
Created August 10, 2014 23:28
Show Gist options
  • Save macikokoro/46e736cdddfc08b529b9 to your computer and use it in GitHub Desktop.
Save macikokoro/46e736cdddfc08b529b9 to your computer and use it in GitHub Desktop.
Thought process for finding the median of even and odd arrays
//Find median with console.log
var numArray = [2, 3, 6, 4, 8, 10, 11, 13, 1, 17, 25];
//sets array in order
numArray.sort(function(a, b) {return a-b;});
//splits array in half
var middle = Math.floor(numArray.length / 2);
console.log("the length in half is: " + middle);
//logs two middle numbers when array length is even
console.log("middle nums when array length is even: " + numArray[middle - 1] + " " + numArray[middle]);
//logs one middle number when array lenght is odd
console.log("middle num when array length is odd: " + numArray[middle]);
//logs contents of array after sort in order
console.log(numArray);
//logs the current length of the array
console.log("length " + numArray.length);
//logs the reaminder if length of array is divided by two
console.log("remainder " + numArray.length % 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment