Last active
August 29, 2015 13:57
-
-
Save shawn01001/9699555 to your computer and use it in GitHub Desktop.
Refactored Code for Group Project -- DBC Phase 0/week 2
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
Nice job with the refactoring. Shortly after I sent off my initial code I realized the sort method wasn't sorting numerically. I took a break and came back to refactor myself after I sent the code to you. I did manage to get it working, but it's nice and hugely beneficial to see how YOU changed it around because it is more concise and there's things I wouldn't ever have thought of. Good job, man