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
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript argument to pick the largest number and break a tie if exists

    Hi there,

    I am trying to pick the larger of 5 numbers and sometimes there is a tie. I would like to prioritize variable c1 if there is a tie. This data is from a quiz I am writing.

    var c1 = 2; // Takes priority if there is a tie.
    var c2 = 1;
    var c3 = 2;
    var c4 = 0;
    var c5 = 0;

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb Something to consider ...

    Can you modify this logic to suit your needs?
    Code:
    <html>
    <head>
    <title>Max Number Tie</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=197967
    
    // array to represent 5 answers to the 'c' question (c1-c5)
    var c = [2,1,2,0,0];
    
    var str = '';
    str += 'Original responses to question "c":<br>';
    for (var i=0; i<c.length; i++) { str += 'c'+(i+1)+'='+c[i]+', '; }
    
    var cmax = [];
    var maxC = -1e10;
    for (var i=0; i<c.length; i++) {
      if (c[i] > maxC) { maxC = c[i]; cmax = [];  cmax.push('c'+(i+1)); }
      else { if (c[i] == maxC) { cmax.push('c'+(i+1)); } }
    }  
    cmax.sort();
    str += '<br>Max response: '+maxC+'<br>';  
    str += cmax.join('<br>');
    
    str += '<br>Priority goes to: '+cmax[0];
    document.write(str);
    </script>
    </head>
    <body>
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Can you modify this logic to suit your needs?
    Code:
    <html>
    <head>
    <title>Max Number Tie</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=197967
    
    // array to represent 5 answers to the 'c' question (c1-c5)
    var c = [2,1,2,0,0];
    
    var str = '';
    str += 'Original responses to question "c":<br>';
    for (var i=0; i<c.length; i++) { str += 'c'+(i+1)+'='+c[i]+', '; }
    
    var cmax = [];
    var maxC = -1e10;
    for (var i=0; i<c.length; i++) {
      if (c[i] > maxC) { maxC = c[i]; cmax = [];  cmax.push('c'+(i+1)); }
      else { if (c[i] == maxC) { cmax.push('c'+(i+1)); } }
    }  
    cmax.sort();
    str += '<br>Max response: '+maxC+'<br>';  
    str += cmax.join('<br>');
    
    str += '<br>Priority goes to: '+cmax[0];
    document.write(str);
    </script>
    </head>
    <body>
    </body>
    </html>
    Thanks a ton this helps out tremendously!

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by brandonlee View Post
    Thanks a ton this helps out tremendously!
    You're most welcome.
    Happy to help.
    Good Luck!


  •  

    Posting Permissions

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