Forum Discussion

FatmaSayed's avatar
FatmaSayed
Community Member
9 years ago

java script

please;;;

I want to know how can i write java script code to get  sum of 5 variables?????????

9 Replies

  • DaveCox's avatar
    DaveCox
    Community Member

    Hi Fatma,

    This will do it.

    var player = GetPlayer();
    var sum = player.GetVar("Variable1") + player.GetVar("Variable2") +
    player.GetVar("Variable3");
    player.SetVar("Sum", sum);

    Replace Variable1, Variable2, Variable3 and Sum with your own variable names in Storyline. I only did three, but you can add as many as you want.

  • FYI:

    you can do the same thing with triggers, using whatever action you are using to initiate the javascript.

    Adjust variable Sum = 0 when user clicks button

    Adjust variable Sum ADD Variable 1 when user clicks button

    Adjust variable Sum ADD Variable 2 when user clicks button

    Adjust variable Sum ADD Variable 3 when user clicks button, etc.

    You just need a trigger for each step (6 triggers) that all fire from the same user action.

     

    The advantage of using triggers is that it is easier for someone who doesn't write JS to do maintenance on the module. Also, triggers function in preview mode, while your module must be published for JS to function.

     

    • FatmaSayed's avatar
      FatmaSayed
      Community Member

      Thanks  for helping Mr/ Walt Hamilton, this is a piece of cake,  and i will use it. 

    • FatmaSayed's avatar
      FatmaSayed
      Community Member

      Thank you so much MR/Richard Hill. :) 

  • DaveCox's avatar
    DaveCox
    Community Member

    Hi Fatma,

    Unless there is another reason you want to do this in Javascript, I suggest it would be better to use Walt's method above. Javascript is very powerful, and can let you do a lot of cool stuff. But, it can be difficult to troubleshoot, and will not run in the preview mode. I only add javascript to my projects when I can't figure out any way to do it in Storyline alone.

  • I just wanted to note, for anyone who might be diving into JavaScript in Storyline, that Dave Cox's solution, at the top of this thread, works just fine here in 2022 with the latest version of Storyline 360 (v3.61.27106.0). Thanks, Dave, for the help. You steered me in the right direction.

    (Now that I've made it work in JS, I'm going to try making it work using Walt's trigger-based solution.)