Copy link to clipboard
Copied
...
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 :
But If I let one TEB without answer, the same problem (string and number) occurs :
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 Correct answer
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
...
Works perfectly !...
Thanks Greg !...
😉
...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Defining a number as default value for a user variable also helps to force recognizing it as a number.

