# How to get the highest value from a Constant Sum type question

Hi,

I have a constant sum question summing up to 100 (percentage)... How can I get the one with the highest value... ?
asked May 7, 2016
You can also view this post which uses SSI Script and Perl ...

https://sawtoothsoftware.com/forum/9318/select-top-2-from-a-constant-sum?show=9320#a9320

This just adds to Zachary's neat idea.

## 1 Answer

0 votes
It's not elegant, but here's an example in JavaScript.  This code prints out the highest value, the position of the highest value, and the label that belongs to the highest value:

```Value: <span id="highestVal"></span><br>
Position: <span id="highestPos"></span><br>
Label: <span id="highestLab"></span>

<script type="text/javascript">
var highestValue = 0;
var positionOfHighestValue;
var labelOfHighestValue;

var cs1 = [% ConstantSumQ_1 %];
if (cs1 > highestValue) {
highestValue = cs1;
positionOfHighestValue = 1;
labelOfHighestValue = '[% ListLabel(ConstantSumQList, 1) %]';
}

var cs2 = [% ConstantSumQ_2 %];
if (cs2 > highestValue) {
highestValue = cs2;
positionOfHighestValue = 2;
labelOfHighestValue = '[% ListLabel(ConstantSumQList, 2) %]';
}

var cs3 = [% ConstantSumQ_3 %];
if (cs3 > highestValue) {
highestValue = cs3;
positionOfHighestValue = 3;
labelOfHighestValue = '[% ListLabel(ConstantSumQList, 3) %]';
}

document.getElementById('highestVal').innerHTML = highestValue;
document.getElementById('highestPos').innerHTML = positionOfHighestValue;
document.getElementById('highestLab').innerHTML = labelOfHighestValue;
</script>
```

You'll need to replace "ConstantSumQ" with the name of your question, and do some copy-and-pasting for the 4th, 5th, 6th, ... list items.

This code is designed to go on a page after the constant sum.  If you want this information on the same page, I'll need to make some modifications.

I'm not sure how you want ties to be broken.  Currently, if two items are tied for highest, the code will go with the earlier list item.
answered May 7, 2016 by Platinum (205,575 points)