Skip to main content
Inspiring
August 11, 2016
Question

Adding variables in JS

  • August 11, 2016
  • 1 reply
  • 278 views

Hello,

Could someone please explain how to add three variable values in javascript for Captivate? I have 3 scoring variables called sc1 sc2 and sc3.  I'd simply like to add them together and set this number into the variable "add_3"

Assuming all the scores are 100, the variable add_3 shows up as 100100100.  

 

window.cpAPIInterface.setVariableValue("add_3", sc1+sc2+sc3);

 

Thanks!

    This topic is closed to new replies. Start a new post to keep the conversation going.

    1 reply

    BDuckWorks
    Inspiring
    August 15, 2016

    Somehow JavaScript is seeing your variables as string values rather than numbers.

    Try this:

    window.cpAPIInterface.setVariableValue("add_3", (sc1 + sc2 + sc3));

    This links shows some examples of this issue, and recommended approaches:

    math - Javascript sign concatenates instead of giving sum of variables - Stack Overflow

    If that doesn't help, these references may be a little more basic:

    JavaScript Tutorial - Lesson 4: JavaScript Operations on Variables

    W3Schools have topic specific lessons:

    JavaScript Operators

    JavaScript Arithmetic

    JavaScript Variables