Skip to content

Instantly share code, notes, and snippets.

@sidonath
Created June 8, 2010 17:46
Show Gist options
  • Select an option

  • Save sidonath/430390 to your computer and use it in GitHub Desktop.

Select an option

Save sidonath/430390 to your computer and use it in GitHub Desktop.

Revisions

  1. sidonath revised this gist Jun 8, 2010. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,19 @@
    Array.prototype.min = Math.min;
    // approach one: simply assign function to prototype
    // it works, but you still need to use "apply" to make the method
    // work as needed
    function smallest1(array){
    Array.prototype.min = Math.min;
    return array.min.apply(array, array);
    }

    function smallest(array){
    return array.min();
    // approach two: use apply in the function assigned to Array's prototype
    function smallest2(array){
    Array.prototype.min = function () { return Math.min.apply(Math, this); };
    return array.min();
    }
    function largest(array){
    return Math.max.apply( Math, array );
    }
    assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
    assert(smallest1([0, 1, 2, 3]) == 0, "Locate the smallest value.");
    assert(smallest2([0, 1, 2, 3]) == 0, "Locate the smallest value.");
    assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");
  2. @jjb jjb revised this gist Jun 7, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    Array.prototype.min = Math.min;

    function smallest(array){
    return array.min;
    return array.min();
    }
    function largest(array){
    return Math.max.apply( Math, array );
  3. @jjb jjb revised this gist Jun 7, 2010. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    Array.prototype.min = Math.min;

    function smallest(array){
    return Math.min.apply( Math, array );
    return array.min;
    }
    function largest(array){
    return Math.max.apply( Math, array );
  4. @jjb jjb created this gist Jun 7, 2010.
    8 changes: 8 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    function smallest(array){
    return Math.min.apply( Math, array );
    }
    function largest(array){
    return Math.max.apply( Math, array );
    }
    assert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
    assert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");