Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2006, 12:42 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default Smallest number

Hi,

I'd like to know how to find out what the 4 smallest numbers in an array would be? And for the life of me I can't seem to find out how to do it.

any ideas would be great...

 
Old January 10th, 2006, 12:59 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi darkhalf,

Try this - it returns an array containing the lowest values...
Code:
var gArr = [6, 9, 5, 3, 8, 5, 10, 35, 1, 99, 2];
var gLowest4ValuesArr = GetLowestValuesArr(gArr, 4);

function GetLowestValuesArr(array, valuesRequired){
    // clone array so we don't upset order in the original
    var cloneArr = new Array(array.length);
    for(var i in array){
        cloneArr[i] = array[i];
    }

    // sort the cloned array & return the required number of values
    cloneArr.sort();
    var valuesToReturn = Math.min(cloneArr.length, valuesRequired);
    var retVal = new Array(valuesToReturn);
    for(var i = 0; i < valuesToReturn; i++){
        retVal[i] = cloneArr[i];
    }
    return retVal;
}
HTH,

Chris

 
Old January 11th, 2006, 12:37 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much... better than I had hoped...

 
Old January 11th, 2006, 02:18 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A follow up question to this... The return array seems to think that 10 is smaller than 5? Does it only look at the first digit?

 
Old January 12th, 2006, 05:07 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry darkhalf, need to supply a sort function to sort numerically...
Code:
cloneArr.sort(SortNums);

...

function SortNums(a, b){
    return a - b;
}
HTH,

Chris






Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
Help: Finding smallest number kris.fan Infopath 0 September 9th, 2007 07:18 PM
Number or parseFloat crmpicco Javascript How-To 6 March 21st, 2006 01:18 PM
window.open smallest width and height crmpicco Javascript How-To 10 July 1st, 2005 09:54 AM
number? crmpicco Classic ASP Basics 2 June 10th, 2005 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.