15 Replies Latest reply on Nov 19, 2012 7:38 AM by GameHermit

    Determine the Highest Two Values


      Hi Everyone!


      So far, with help from this great forum and via Google-fu I've pretty much been able to figure almost all of my calculation questions out but I think I've finally met my match and need some help .


      I am working on automating a charater sheet for a roleplaying game. There are 8 skills that have a value of 1 - 6 each. What I need to do is determine the highest TWO values, compare each to a table to determine the bonus values then add the two bonus values together.


      I can easily find the highest value using Math.max() but I also need the second highest number....the two highest numbers could be the same. For example, the two highest numbers could be 6 and 6.


      Here's what I have to determine the highest value:


      var cl = this.getField("ClericLevel").value;

      var el = this.getField("EnchanterLevel").value;

      var ll = this.getField("LeaderLevel").value;

      var scl = this.getField("ScholarLevel").value;

      var stl = this.getField("ScoutLevel").value;

      var spl = this.getField("SpellcasterLevel").value;

      var tl = this.getField("ThiefLevel").value;

      var wl = this.getField("WarriorLevel").value;

      var highestlevel = Math.max(cl,el,ll,scl,stl,spl,tl,wl);


      Next, I can determine the "bonusvalue" for the above highest value by using the following:


      if ( highestlevel <= 1){

      event.value = 0;

      } else if ( highestlevel >=2 && highestlevel <= 3){

      event.value = 1;

      } else if ( highestlevel >=4 && highestlevel <= 5){

      event.value = 2;

      }else if (highestlevel == 6){

      event.value = 3;



      Now what I need to do is actually use the above if else statements to create a var for the highest value's "bonuslevel" and then again for the second highest value's bonus level...then add the two bonus levels together.


      So for a complete example...lets say that I have the following values (4,2,3,1,6,6,5,4). The highest number would be 6 and (based on the if else statements above) has a "bonuslevel" of 3. The second highest value is also 6, so again has a "bonuslevel" of 3. the sum of these two bonuslevels is 6. So, for this example, the event.value = 6.


      I just can't figure out how to pull in the second highest number as a second variable and determine the "bonuslevel" for that second variable.


      Thank you so much for looking at this with me...I really appreciate any help offered


      ~ Jack