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

Javascript - Add many numbers (Variables)

Engaged ,
May 26, 2021 May 26, 2021

...

Hello !...

I'm working on a game project and I have a strange issue...

I'm collecting numbers in TEB (and saved them as variables named "D1_1", "D1_2", ... , "D1_6")

And I want to add these 6 variables, the result would be another variable ("Total_1")...

(Of course I can add them using the Captivate's Expression action but it would add more variables, need 5 actions... So I wanted to use Javascript !...)

And to add these 6 variables with Javascript it would need  just a line of code :

Total_1 = D1_1 + D1_2 + D1_3 + D1_4 + D1_5 + D1_6;

After having a problem (my variables considered as string and not as numbers), I looked in Js ressources on Internet and found that I have to add :

Number(Total_1);

And it seems to be ok :

Yam_01.pngexpand image

 

But If I let one TEB without answer, the same problem (string and number) occurs :

 

Yam_02.pngexpand image

 

So... I need your help !

(I already found a solution by adding the default text of "0" in the TEB... But I would  prefer that the TEBs were cleared...)

Thanks in advance !...

😉

...

Edit : I forget to precise that I affect all these variables with "0" when entering the slide...

...

1.5K
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

correct answers 1 Correct answer

Advisor , May 26, 2021 May 26, 2021

Here is a thought. Without examining more closely, this may take care of things for you.

You can also create a number by performing a math function. I have often done this quick hack to ensure that I have numbers. Just multiply each value by 1 so that the value does not change but it will become a number. Then you can prevent the concatenation.

 

varTotal = (varA*1)+(varB*1)+(varC*1)+(varD*1)...etc

Translate
Advisor ,
May 26, 2021 May 26, 2021

Here is a thought. Without examining more closely, this may take care of things for you.

You can also create a number by performing a math function. I have often done this quick hack to ensure that I have numbers. Just multiply each value by 1 so that the value does not change but it will become a number. Then you can prevent the concatenation.

 

varTotal = (varA*1)+(varB*1)+(varC*1)+(varD*1)...etc

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
Engaged ,
May 26, 2021 May 26, 2021

...

Works perfectly !...

Thanks Greg !...

😉

...

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
New Here ,
Aug 06, 2021 Aug 06, 2021

This is something I am trying to do. I am such a newbie when it comes to Javascript. 

I am confused as to how to get this to work. Is there some other items to put into it? I have done some research and see things like

window.cpAPIInterface.setVariableValue("variable", (var1 + var2 + var3 + var4));

 

This has not worked. Ready to pull out my hair. 

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 06, 2021 Aug 06, 2021

If you're just using the Captivate Execute JavaScript window, try something like...

 

varTotal = (var1 + var2 + var3);


I will suggest you make all your variables in Captivate.

Then make sure that you have assigned values to vars 1, 2, and 3.

 

If something is not working the way you want - I recommend creating troubleshooting boxes.

Do this by making 4 smartshapes and place text in them as such...

$$var1$$

$$var2$$

$$var3$$

$$varTotal$$

Then you can see which values are not properly updating and zero in on the problem.

 

Hopefully this helps.

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
Community Expert ,
Aug 07, 2021 Aug 07, 2021
LATEST

To avoid typos (variables are case sensitive) I always recommend to use the X button for inserting variables in a text container (caption or shape) instead of typing in text.  Sorry for the pop in.

InsertVar.PNGexpand image

Defining a number as default value for a user variable also helps to force recognizing it as a number.

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