Solved

Sum Variables

I’d like to take five variables (each variable tracks and reports out a different type of skill) and add the totals of all five variables to a sixth variable (which would then report out a total score). I am having problems getting that work. I'm starting to wonder if it's even possible to add multiple variables’ totals to another variable. If it is possible, could someone explain how to do that?

Solution

Yes, you can add multiple variables within Lectora in a few different ways, here are two:

You can use the Lectora action Modify Variable to add a value to another variable's value. Given a variable named runningTotal and another named Two, you would do an action like:

modVar

Target: runningTotal

Type: Add to Variable

Value: VAR(Two)

If you wanted to add five variables to this one, you would have five actions, with the Value field changing to the name of each variable.

Another method to add multiple variables to another is using javascript. Let's use three variables you define in Lectora named; One, Two, and Three, and the other named runningTotal. You could have an action that runs some JavaScript. One way to code it is as follows, assuming the addition is numerical and not adding characters to a string:

runJavascript

JavaScript: VarrunningTotal.set( parseInt(VarOne.getValue(),10) + parseInt(VarTwo.getValue(),10) + parseInt(VarThree.getValue(),10) );

Or if the action will run more than once and you want to keep adding to the runningTotal variable, then add its present value to the formula:

JavaScript: VarrunningTotal.set( parseInt(VarrunningTotal.getValue(),10) + parseInt(VarOne.getValue(),10) + parseInt(VarTwo.getValue(),10) + parseInt(VarThree.getValue(),10) );

The Lectora action method is easier within Lectora. The JavaScript method lets you fine-tune your code and use the power of JavaScript to build on the flexibility of Lectora. 

Discussion (2)

To add to what @jholland wrote, something I wasn't sure of when I started using Lectora: actions always run in the order they are listed in your course, if they have the same trigger. That is, all OnShow actions attached to a page happen in the order they're listed in (as an example). This isn't guaranteed in all environments, but it's the case here.

Yes, you can add multiple variables within Lectora in a few different ways, here are two:

You can use the Lectora action Modify Variable to add a value to another variable's value. Given a variable named runningTotal and another named Two, you would do an action like:

modVar

Target: runningTotal

Type: Add to Variable

Value: VAR(Two)

If you wanted to add five variables to this one, you would have five actions, with the Value field changing to the name of each variable.

Another method to add multiple variables to another is using javascript. Let's use three variables you define in Lectora named; One, Two, and Three, and the other named runningTotal. You could have an action that runs some JavaScript. One way to code it is as follows, assuming the addition is numerical and not adding characters to a string:

runJavascript

JavaScript: VarrunningTotal.set( parseInt(VarOne.getValue(),10) + parseInt(VarTwo.getValue(),10) + parseInt(VarThree.getValue(),10) );

Or if the action will run more than once and you want to keep adding to the runningTotal variable, then add its present value to the formula:

JavaScript: VarrunningTotal.set( parseInt(VarrunningTotal.getValue(),10) + parseInt(VarOne.getValue(),10) + parseInt(VarTwo.getValue(),10) + parseInt(VarThree.getValue(),10) );

The Lectora action method is easier within Lectora. The JavaScript method lets you fine-tune your code and use the power of JavaScript to build on the flexibility of Lectora.