Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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: