Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding variables in JS

Explorer ,
Aug 11, 2016 Aug 11, 2016

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!

258
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Aug 15, 2016 Aug 15, 2016
LATEST

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources