# Variable Tutorial with Calculations

Aug 01, 2012

I was watching a tutorial by Jeanette titled Adding Data Entry Fields. The tutorial did a great job of showing how to enter text fields. The example she used also showed how it is possible to have some numberical entries for the user and at the end it would calculate a result.

The example was about a 401(K) plan and how the employee could enter a dollar amount, number of years and a percentage to get a result. Is there a tutorial that covers a similar type of calculation?

I did look at the tutorial for Adding Number Variables but it only demonstrated how to assign a numerical value for a choice. It did not show how to do a calculation.

Thanks for the tutorials and I hope there is one for calculations or that you can make one.

Jeffrey Riley

###### 23 Replies

Hi Jeffrey! I did a quick screenr on how the calculation slide was built, but maybe it would help to also take a peek at the slide itself so you can deconstruct:

Screenr

Published demo

Source file

The slide asks the learner to enter 3 items: their salary, the percent they plan to contribute to their 401k, and the number of years till retirement. When they click the Calculate button, a layer appears which shows them the total amount of matching funds the company will contribute on their behalf.

Here's the logic behind the calculations:

• I create a variable called Match Calc which is used to display the total on the Results layer. I also created variables for the match percent, salary, and years till retirement.
• I have a trigger on the base layer which initially sets my Match Calc value equal to the salary, but then on the results layer we perform some calculations on that number to get to the final result.
• When the learner hits the Calculate button, triggers on the Results layer take the salary, multiply it by the matching percentage, and then multiple by the number of years till retirement.

I also have a different layer which pops up if the learner tries to hit the Calculate button without entering all the required information.

I hope that helps!

That was just what I wanted. I will know I have this down when I can make my own examples by stepping through the logic.

Thanks to all of you who do the tutorials, GREAT JOB!

Thanks for asking the question Jeffrey and thanks for the example Jeanette.

I built a similar tool and I have it working fine.  The user enters 3 numbers and they perform the calculation.  Is there a way to reset the fields so that they can try again with different numbers?  Ideally I'd like them to click a Reset(!) button so it clears the values and they can play with as many numbers as they want.

Add a reset button that sets all the variables to 0

Hi David! Phil's idea is a good one - if your interaction is like the the 401k example posted above, you could perhaps add some triggers to the "Thanks" button on the layer called Results. That way, learners could close the layer and reset the values all with one click. Here's how the triggers would look:

Thanks Phil and Jeanette!  Works like a charm.

Can we revisit this for a bit please? I'm building a calculation much like this and it's working. But is there a way to format the numbers, i.e. 4,000 instead of 4000?

Hi Carrie! I saw this discussed in a previous thread and I think that this is what you are looking for.

Hi, All. I have a variation on this thread. I have the learner enter a single-digit number in a numeric data entry box on seven different slides. On the final slide, I want those seven numbers to total into the last box. Can you help me with that, please?

Thanks - Melissa

Hi Melissa, have a look at the attached sample and see if that's what you want. The final slide has a bunch of triggers that add the values of the seven data entries from previous slides into one total variable.

Hi Melissa,

It looks like Michael has helped you out, and if you need anything else please let us know.

Genius, Michael! Thanks so much. Melissa

Hello, I am working with a numeric entry and need three decimal places. When I am creating  a condition for a trigger it will automatically round the decimal to two places. How can I change this to three decimal places with not rounding?

Hi Brent,

This thread is a bit older, so I'm not certain if folks are still subscribed so you may want to message them directly using the "contact me" button on their profile to see if they're able to share what they ended up doing. You may want to also look at investigating what options you have if you were to use Javascript to work with multiple decimal points in variables. It may be something the community can assist with, but you may want to begin by reviewing the best practices here.

can i get result total in php ya javascript variable.

Hi Rakesh,

I just responded to your question here, and this conversation may shed a little more light on what you want to do.

Tracking in a text file or database is not something I can assist in supporting. As I mentioned in the other conversation, we recommend publishing to an LMS.

Hopefully, someone in the community will be able to chime in and help you out here.

I have data input fields where user can input numbers, and I have it set to calculate the numbers automatically without clicking a "calculate button."

For example:

When a numeric input loses focus, set to typed value and add to the tally.

Or alternatively, when numeric input variable changes, add the variable to the tally.

How can I set the tally variable to adjust properly when a user changes an input field. I get how the calculation button works and can make adjustments by setting the tally variable to zero when clicked and placed at top of trigger list, but, I don't want to use a calculate button. I want it to tally automatically. Any ideas?

Laura Ingle

I don't want to use a calculate button. I want it to tally automatically

Hi Laura

unfortunately for any input field SL waits for the field to lose focus before it validates the entry...so the user needs to move from that field using some action. I'd love for it to calc automatically...if anyone has worked out how to do it I'd be interested as well.

Thanks for sharing! 😊

Get Outlook for Android

Thanks Wendy! Unfortunately, I can't get that JS solution link you provided to work in this case.

Here's a sample (attached) of what I'm trying to do. I have this calculating automatically. And it works great, except for when you need to change one of the input fields.

Hi Laura

I know what you're trying to do but it won't work with the way SL works until the field loses focus so when the user gets to the last field unless they do something outside of that field it won't calculate. Also if you go back and change a field it calculates when you move to another field but if you happen to click in other fields (similar to tabbing fields) it keeps calculating. It's a limitation of the way SL validates entry fields so I think JS will be your only option.

Now that you've shared the slide someone more technical than me maybe able to help @philmayor @owenholt

I agree. It's going to take some scripting to make this work. I've tried so many ways around it to reset the variables (on clicks of input, on press of key, tried playing around with some JS, etc.) and to no avail. Then I get the NaN values. Eek. I'm not really good at JS, so hopefully by putting the file out there I can get more feedback. Someone has to have done this before. Thanks for your help! I appreciate your support! 😊

Get Outlook for Android