Hey everyone,

So I'm creating a stress management activity where the learner would read a series of statements and then rate each one between 1-5 depending how they feel about it.

I'm looking at using Java Script to add up to 35 variables together and then provide a total number where I can give customised feedback.

I've just been testing solutions to come up with the most efficient solution

My current workable solution looks like this:

var player = GetPlayer();
var num1 = player.GetVar("var1");
var num2 = player.GetVar("var2");
var num3 = player.GetVar("var3");
var num4 = player.GetVar("var4");
var num5 = player.GetVar("var5");
var add = num1 + num2 + num3 + num4 + num5;

My issue with this is that I have to write out the num1 + num2 etc 35 times which is not ideal. I want to use a loop to do it for me, but when I test my loop code, it doesn't do the calculation.

var player = GetPlayer();
var num1 = player.GetVar("var1");
var num2 = player.GetVar("var2");
var num3 = player.GetVar("var3");
var num4 = player.GetVar("var4");
var num5 = player.GetVar("var5");

for (i = 1; i <= 5; i++){
}

Can anyone suggest ways I could clean up my code or even get my loop working as intended?

Thanks very much,

Alex

###### 6 Replies Not saying that this can't be done with javascript, but curious why not just use Storyline's built in functionality to do this instead of going to the complication of using javascript?

What you're trying to do can easily be accomplished in Storyline without the javascript.

For each statement your users are providing a rating for, when that rating is given, add the value to a "total" variable.  At the end, when all of the ratings have been given you will have a total value already calculated. Hi, Alex -- Looks like Brian is providing you with some really great assistance here! Please let us know if his suggestion without employing Javascript will work for you, or if you are certain you'd like to use JS as an option, unfortunately, that is not something for which we could provide assistance. Luckily, we have lots of community members who are more than willing to share their expertise to assist! Hey Brian,

Thanks for writing back. You're absolutely right in that it can be done using Storyline triggers, and for smaller calculations I would definitely use that approach.

I'm currently trying to learn and utilise Javascript in Storyline to both increase the complexity of what I can develop and hopefully speed up the process.

My thinking here was If I could do the same thing in one trigger as I could in 35 that would be preferable. It would also offer flexibility if for whatever reason I needed to subtract or multiply the results instead, I could make one change rather than go into each trigger and re adjust.

I had a bit more of a play around and managed to get it working. For some reason when I took the extra step of putting the values into an array it allowed me to loop through all the variables to do the calculation.

var calc = 0;
var player = GetPlayer();
var num1 = player.GetVar("var1");
var num2 = player.GetVar("var2");
var num3 = player.GetVar("var3");
var num4 = player.GetVar("var4");
var num5 = player.GetVar("var5");
var array = [num1, num2, num3, num4, num5];

for(var i=0; i < array.length; i++){

calc += array[i];
}
player.SetVar("total", calc);

I've uploaded a demo and the storyline file in case anyone is interested in trying to do something similar with Javascript. Hopefully this can help you get started.

Demo: http://goo.gl/dBOuCb

Storyline File: https://goo.gl/kzIdPI Actually, I experienced some issues with Alex's code.

His returns a list of the numbers, but not added together, so I made sure to convert all of the variables to numbers so they would add instead of concatenate (a fancy JS term meaning push together into one text string).

A simple fix would be using the built-in Number() function when calling the variables.

var calc = 0;

var player = GetPlayer();
var num1 = Number(player.GetVar('score1'));
var num2 = Number(player.GetVar('score2'));
var num3 = Number(player.GetVar('score3'));
var num4 = Number(player.GetVar('score4'));
var array = [num1, num2, num3, num4];

for(var i=0; i < array.length; i++){

calc += array[i];
}
player.SetVar("total", calc);