Created
August 10, 2014 23:59
-
-
Save macikokoro/296d77ae80ced86b00bb to your computer and use it in GitHub Desktop.
function that finds the median of a set of numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Find the median | |
var numArray = [2, 3, 6, 4, 8, 10, 11, 13, 1, 17, 25, 200]; | |
numArray.sort(function(a, b) {return a-b;}); | |
//log the contents of the array | |
console.log(numArray); | |
//log the array length | |
console.log("array length is: " + numArray.length); | |
//Split the array in half | |
var middle = Math.floor(numArray.length / 2); | |
//function to find the median | |
function findMedian () { | |
if(numArray.length % 2 === 0) { | |
//log the two middle numbers if array length is even | |
console.log("middle nums when array length is even: " + numArray[middle - 1] + " " + numArray[middle]); | |
//log the result after dividing the two middle numbers | |
console.log("the median is: " + (numArray[middle - 1] + numArray[middle])/2); | |
} else { | |
console.log("middle num when array length is odd: " + numArray[middle]); | |
} | |
} | |
//call function | |
findMedian(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment