For this example, we'll calculate the total number of months in a specified number of years.
1. Create a page in Lectora
First, create a blank project in Lectora or create a new page in an existing project.
2. Add the Required Objects
Add a text box. The text box can sometimes be hard to find. Go to Add >> Object >> Form Object >> Entry Field. Name the textbox "Years". Name the associated variable "Years" also.
Add a button. Label the button "Calculate Months".
Add a label. This is where we will but the results. Name it "Months".
You should now have something that looks similar to the following:
3. Create the Action Group
Create an action group. Name it "Calculate Months". Your tree view in Lectora should now look something like the following:
You'll be adding two actions to the action group. The first action will calculate the months. The second action will update the page with the new value. You will need to make sure these are ordered as stated to display the correct results.
Add a Modify Variable Action. This is where you will do the calculation of years to months by multipling by 12. Notice the calcuation will change the value of the variable "Years" to months. Set the following:
- Action Name: Multiply by 12
- Action: Modify Variable
- Target: Years
- Value: 12
- Modification Type: Multiply Variable By
Add a Change Contents Action. This will display the resultant number of months (now in the variable "Years") to the learner. Set the following:
- Action Name: Update Months
- Action: Change Contents
- Target: Months
- New Content: Years
4. Link the Action Group to the Button
Now you will need to go back to the button and edit it so that when the learner clicks it, the calculation will be made. Go into the button properties and choose the "On Click" tab. Set the following:
- Action: Run Action Group
- Target: Calculate Months
5. Preview the Project
All that is left to do is to see the code in action. Preview the project and notice any number you put in will be calculated into months when you click the button.
What happens if you don't put in a number? The calculation will not work and the text will show up in the results.