-
-
Save aubuchcl/9731303 to your computer and use it in GitHub Desktop.
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
// refactored code | |
// first set of functions received from Jake | |
// sum function worked upon receiving; did not modify | |
var sum = function(array){ | |
var total = 0; | |
for(var index in array) | |
total += array[index]; | |
return total; | |
} | |
// mean function worked upon receiving: did not modify | |
var mean = function(array){ | |
var sum = 0; | |
for(var index in array){ | |
sum += array[index]; | |
} | |
return sum / array.length; | |
} | |
// median fuction worked upon receiving: cleaned up code | |
function median(array){ // modified code: replaced var median = function(array) with current code | |
array.sort(function(x,y) {return x - y}); // modified code: replaced sorted with array | |
var middle = (array.length - 1) / 2; // modified code: changed var mid_index to var middle | |
if(array.length % 2) | |
return array[middle]; | |
else | |
return (array[Math.floor(middle)] + array[Math.ceil(middle)]) / 2.0; | |
} | |
// second set of functions received from Devin | |
// sum function worked upon receiving: did not modify code | |
function sum(array) { | |
var sum = 0; | |
for (var i=0; i<array.length; i++){ | |
sum += array[i]; | |
} | |
return sum; | |
} | |
// mean function worked upon receiving: removed one line of code | |
function mean(array) { | |
var mean = 0; | |
for (var i=0; i<array.length; i++){ | |
mean += array[i]; | |
} //modified code: combined mean /= array.length and return mean; | |
return mean /= array.length; | |
} | |
// median function didn't work upon receiving: modified to work and cleaned up code | |
function median(array) { //removed line of code | |
array.sort( function(x,y) {return x - y}); //added function to sort numerically | |
var middle = Math.floor(array.length / 2) // modified to remove erroneous code | |
if (array.length % 2) | |
return array[middle]; | |
else | |
return (array[middle - 1] + array[middle]) / 2.0; // modified code: 2 to 2.0 | |
} | |
/* user stories: | |
1. I need a way to add together a set of numbers and return the sum. | |
2. I need a way to return the average of a set of numbers. | |
3. I need a way to return the median of a set of numbers, | |
making sure to return the middle number if the set of numbers is odd, | |
or the division of the two innermost numbers if the set is even. | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment