Scoring a test and writing results to variables

Hi,

I’m creating an eLearning course that includes a questionnaire with 9 scales. Based on user input, each scale will have a numeric value of 0 - 8 (stored in scale variables scale1, scale2, scale3,…scale9). So far, so good. I can store the values in the variables just fine.

The next part is beyond me and I don’t believe it’s possible with the built-in scoring functionality of AP. What I need to do is determine which scale (or scales) has the highest value and write the value to a new variable that includes the scale name and value (e.g., score = scale2, value).

Finally, if there is a single high score (e.g., scale7 = 8 and all other scales are < 8), I need to determine which of the adjacent scales has the higher value. So, if scale7 = 8 is highest, I need the adjacent scale with the higher score written to a variable (e.g., scaleSideHigh=Six or scaleSideHigh=Eight or scaleSideHigh=Tied).

This is probably easy for you crack programmers, but sadly my JS abilities are near nil. Any help you can provide will be greatly appreciated.

Thanks,
SP

Hi Scott,

You can use the following script to do that:

var numberOfScales = 9,
  scaleVarPrefix = 'scale',
  scaleVars = [],
  i;

// initialize list of scale variables
for (i = 1; i <= numberOfScales; ++i) {
  scaleVars.push(scaleVarPrefix + i);
}

// sort scale variables in descending order of value
scaleVars.sort(function(a, b) {
  return prez.variable(b) - prez.variable(a); 
});

// after sorting, scales with highest value are at the beginning of scaleVars list
var highestValue = prez.variable(scaleVars[0]);
// score has format "highest value, comma seperated list of scales with that value"
// e.g. 8, scale1, scale6 (scale 1 & scale 6 both have highest value 8)
var score = highestValue;
i = 0;
while (i <  numberOfScales - 1 && prez.variable(scaleVars[i]) == highestValue) {
  score += ', ' + scaleVars[i];
  ++i;
}
prez.variable('score', score);
// scaleHigh: comma separated list of scale variables that have highest value
var scaleHigh = score.split(', ').slice(1).join(', ');
prez.variable('scaleHigh', scaleHigh);

// adjacent higher value
if (prez.variable(scaleVars[1]) < highestValue) {
  // only variable scaleVars[0] has highest value
  var varNumber = parseInt(scaleVars[0].charAt(scaleVars[0].length - 1));
  var prevVarName = scaleVarPrefix + (varNumber > 1 ? varNumber - 1 : numberOfScales);
  var nextVarName = scaleVarPrefix + (varNumber < numberOfScales ? varNumber + 1 : 1);
  var prevValue = prez.variable(prevVarName);
  var nextValue = prez.variable(nextVarName);
  if (prevValue != nextValue) {
    // scaleSideHigh has format: value, name 
    if (prevValue < nextValue)
      prez.variable('scaleSideHigh', nextValue + ', ' + nextVarName);
    else
      prez.variable('scaleSideHigh', prevValue + ', ' + prevVarName);
  } else {
	// two adjacent values are the same
	prez.variable('scaleSideHigh', 'Tied');
  }
} else {
  // more than one variables have highest value
  // not sure how you need in this case, just scaleSideHigh to Tied as an example
  prez.variable('scaleSideHigh', 'Tied');
}

Regards

Hello Toan Le:

First, thank you so much. I’m very grateful for the wonderful help and quick response. The scoring is working as I requested, but unfortunately I forgot to mention 2 things: (1) the adjacent side scales for scale9 are scale8 and scale1 (because the scheme for the scales is circular so that 9 is between 8 and 1), and (2) the scaleSideHigh variable needs to indicate the scale name (similar to the score variable with scale, score).

I really can’t wait to see what’s in AP v. 8 and will support your company in every way I can. Our team has chosen AP over competitive alternatives based on ideal combo of features, price, and support (the latter is the best I’ve come across in 2 decades in the learning field).

Many many thanks!
SP

I’m sorry for misunderstanding about adjacent side scales.
I’ve updated the script in my previous post, please check it again.

Regards

Many thanks, Toan Le. The code works perfectly!

Here’s wishing you much joy–
Scott

Hi again, Toan Le. I have and one more quick question (and promise it’s the last one on this : ). Is there an easy way to generate one additional score variable (e.g., score2) that contains just the name of the highest scale without the numerical value included in the variable. For example, if the test result was a single highest score of 8 on scale 9 it would be reported in variable score = “8, scale9” (as in the current formulation) and (2) variable scaleHigh = “scale9”?

Thanks so much, again, and kind regards!
SP

Hi Scott,

You just need to add these two lines of code after prez.variable('score', score);

var scaleHigh = score.split(', ').slice(1).join(', ');
prez.variable('scaleHigh', scaleHigh);

For your convenience, I’ve updated the script in my first answer.

Regards

Hi Toan Le,

Once again, I thank you so much for generously sharing both your time and knowledge.

Kind regards!
Scott