How to find maximum value among X number of points?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pyrokat
    New Member
    • Jan 2010
    • 5

    How to find maximum value among X number of points?

    This is all i have right now and i cant figure out how to go further..

    Code:
         var numEntries;
                        var max;
                        var pt;                                       
                        
                        numEntries = Number(prompt("Number of entries to process?"));
                          
                        do {                
                            pt = Number(prompt("Enter data point");
                            numEntries = numEntries - 1
                            
                        while ((numEntries - 1) !== 0);
                        }
    Last edited by Dormilich; Jan 12 '10, 05:07 AM. Reason: Please use [code] tags when posting code
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    you might split that into 2 steps ... let me give you an idea for that:
    Code:
    var numEntries;
    var max;
    var pt = [];                                       
     
    numEntries = parseInt(
        prompt("Number of entries to process?"), 10
    );
     
    for (var i = 0; i < numEntries; ++i) { 
        pt[i] = parseFloat(prompt("Enter data point"));
    }
    
    alert(pt.toString());
    
    // so now process the max finding here for the array pt
    kind regards

    PS: you could even use one step of course ... just remember the bigger values through the inputs in a variable like this:
    Code:
    if (typeof max == 'undefined' || pt[i] > max) {
        max = pt[i];
    }

    Comment

    • xNephilimx
      Recognized Expert New Member
      • Jun 2007
      • 213

      #3
      You can use Math.max() to find the max number like:

      [CODE=javascript]
      max = eval("Math.max( "+pt.join(',')+ ")");
      [/CODE]

      Comment

      Working...