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 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

• ###### 1. Re: Determine the Highest Two Values

If you put those values into an array, you can easily sort them numerically, hightest to lowest, and then use the first two elements of the array. Do a Google search for "numeric sort array JavaScript" to see how this is done. If you get stuck, post again.

• ###### 2. Re: Determine the Highest Two Values

Thanks for the reply George...I'll definitely give that a go

~ Jack

• ###### 3. Re: Determine the Highest Two Values

OK...I found out how to sort the array...but I'm not sure how to assign the two highest values to two variables.

Example if I use:

var levels = new Array (1,3,2,4,6,6,5,2);

levels.sort(compare);

The result should be: (1,2,2,3,4,5,6,6) with 6 and 6 being my highest two values. Is there a way to assign those two values to two variables so that I can add them together? Something like:

var h1 = 6;

var h2 = 6;

event.value = h1 + h2;

Sorry...I'm really new at this.

Thanks,

Jack

• ###### 4. Re: Determine the Highest Two Values

If you define your sorting function to sort the numbers in a descending order, you can simply access levels[0] and levels[1] after running it to get the two highest values.

• ###### 5. Re: Determine the Highest Two Values

OK cool...so if I do this:

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

var el = this.getField("EnchanterLevel").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 levels = [cl,el,ll,scl,stl,spl,tl,wl];

levels.sort(function(a,b){return b - a});

Will this return the values in decending order (not sure about the syntax)?

Then it would be:

event.value = levels[0] + levels[1];

Is this close?

• ###### 6. Re: Determine the Highest Two Values

That should do the trick, yeah...

• ###### 7. Re: Determine the Highest Two Values

Cool....I'll give it a go and let you know how it turns out

• ###### 8. Re: Determine the Highest Two Values

I would explicitly convert each field value to a number if there's a chance any of them could be blank.

• ###### 9. Re: Determine the Highest Two Values

Worked Beautifully!!!! Thanks George!

Here's the final code if it is of value to anyone:

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

var el = this.getField("EnchanterLevel").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 levels = [cl,el,ll,scl,stl,spl,tl,wl];

levels.sort(function(a,b){return b - a});

if ( levels[0] <= 1){

var h1 = 0;

} else if ( levels[0] >=2 && levels[0] <= 3){

var h1 = 1;

} else if ( levels[0] >=4 && levels[0] <= 5){

var h1 = 2;

}else if (levels[0] == 6){

var h1 = 3;

}

if ( levels[1] <= 1){

var h2 = 0;

} else if ( levels[1] >=2 && levels[1] <= 3){

var h2 = 1;

} else if ( levels[1] >=4 && levels[1] <= 5){

var h2 = 2;

}else if (levels[1] == 6){

var h2 = 3;

}

event.value = h1 + h2;

• ###### 10. Re: Determine the Highest Two Values

How do I explicitly convert the field value? Just set it to a number?

• ###### 11. Re: Determine the Highest Two Values

A simple way is to use the unary + operator:

var n1 = +getField("text1").value;

If the field is blank, this will convert the value (an empty string) to zero.

• ###### 12. Re: Determine the Highest Two Values

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

Use:

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

Is this correct?

• ###### 13. Re: Determine the Highest Two Values

That's right.

• ###### 14. Re: Determine the Highest Two Values

WOOT!

Thanks George

• ###### 15. Re: Determine the Highest Two Values

OK...made the change and it worked great!:

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

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

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

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

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

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

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

var levels = [cl,el,ll,scl,stl,spl,tl,wl];

levels.sort(function(a,b){return b - a});

if ( levels[0] <= 1){

var h1 = 0;

} else if ( levels[0] >=2 && levels[0] <= 3){

var h1 = 1;

} else if ( levels[0] >=4 && levels[0] <= 5){

var h1 = 2;

}else if (levels[0] == 6){

var h1 = 3;

}

if ( levels[1] <= 1){

var h2 = 0;

} else if ( levels[1] >=2 && levels[1] <= 3){

var h2 = 1;

} else if ( levels[1] >=4 && levels[1] <= 5){

var h2 = 2;

}else if (levels[1] == 6){

var h2 = 3;

}

event.value = h1 + h2;