Skip to content

Instantly share code, notes, and snippets.

@shawn01001
Last active August 29, 2015 13:57
Show Gist options
  • Save shawn01001/9699555 to your computer and use it in GitHub Desktop.
Save shawn01001/9699555 to your computer and use it in GitHub Desktop.

Revisions

  1. shawn01001 revised this gist Mar 22, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions gc1_refactored.js
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,6 @@ var sum = function(array){
    total += array[index];
    return total;
    }

    var number_list = [3, 8, 9, 1, 5, 7, 9, 21];
    sum(number_list);

    // mean function worked upon receiving: did not modify
    var mean = function(array){
  2. shawn01001 revised this gist Mar 22, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gc1_refactored.js
    Original file line number Diff line number Diff line change
    @@ -65,3 +65,5 @@ function median(array) { //removed line of code
    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.
    */

  3. shawn01001 created this gist Mar 22, 2014.
    67 changes: 67 additions & 0 deletions gc1_refactored.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    // 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;
    }

    var number_list = [3, 8, 9, 1, 5, 7, 9, 21];
    sum(number_list);

    // 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.