Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smallest Positive Number

    I know to get the smallest number is quite simple,Math.min(-6,5,12), and you would get -6.

    But from that example how would you go about getting 5?

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Md8^h2nx View Post
    I know to get the smallest number is quite simple,Math.min(-6,5,12), and you would get -6.

    But from that example how would you go about getting 5?
    One way would be to use apply to pass the parameters as a filtered array:

    alert( Math.min.apply( null, [-6,5,12].filter( function( e ){ return e > 0; } ) ) );

  • Users who have thanked Logic Ali for this post:

    Md8^h2nx (06-05-2013)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Simpler but still effective:-


    Code:
    <script type = "text/javascript">
    
    var myarray = [-6, 5,12, 3,-34];
    var min = 1e20;  // a big number
    for (var i=0; i<myarray.length;i++) {
    if ((myarray[i] >= 0) && (myarray[i] < min)) {
    min = myarray[i];
    }
    }
    alert ("The lowest positive value in the array is " + min);
    
    </script>
    Quizmaster: "Far From The Madding Crowd" is a novel by which author?
    Contestant: Ian Fleming

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Yeah, but how do you do it WITHOUT using an array?

    Only way I see would be to write a simple function:
    Code:
    function minAbs( )
    {
        var args = Array.prototype.slice.call(arguments);
        args = args.filter( function(e) { return e > 0; } );
        return Math.min.apply( null, args );
    }
    alert( minAbs( -6, 5, 12 ) );
    And of course the 3 lines in that function can be reduced to one line, but I prefer to leave it like that for the sake of clarity.

    Of course, if you want it to run in all browsers--including old MSIE that doesn't have filter--you could just do it the dirt simple way:
    Code:
    function minAbs( )
    {
        var min = null;
        for ( var a = 0; a < arguments.length; ++a )
        {
             var n = arguments[a];
             if ( n > 0 && ( min === null || n < min ) ) min = n;
        }
        return min;
    }
        
    alert( minAbs( -6, 5, 12 ) );
    And I wouldn't be at all suprised to find that's at least as fast as using filter and apply and slice and call in the much harder to understand version.

    N.B.: If you want to find zero as a possible minimum, change n > 0 to n >= 0 (same applies to the filter version where you would use e >= 0).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •