Problems calculating numeric using java script ...
Jun 11, 2020
Hello, just wan t to calculate some numbers using JavaScript in a trigger --- but it seems, that the script give back a string :-(
I have the numeric entry e.g. "1" and "2" and "3" and i wan t to get as a result "6" because 1 + 2 + 3 = 6 but i get this 123 :-(
----
Why?
//Hallo Axel - schoen dass du hier bist - lass uns mit dem Player reden ... da gibt es was zu tun .... und zwar das hier ...
var player = GetPlayer();
//hier hast du die Varibalen - schreibe die mal um kurz
var NPS_0 = player.GetVar("NPS_ist_00"),
NPS_1 = player.GetVar("NPS_ist_01"),
NPS_2 = player.GetVar("NPS_ist_02"),
NPS_3 = player.GetVar("NPS_ist_03"),
NPS_4 = player.GetVar("NPS_ist_04"),
NPS_5 = player.GetVar("NPS_ist_05"),
NPS_6 = player.GetVar("NPS_ist_06"),
NPS_7 = player.GetVar("NPS_ist_07"),
NPS_8 = player.GetVar("NPS_ist_08"),
NPS_9 = player.GetVar("NPS_ist_09"),
NPS_10 = player.GetVar("NPS_ist_10");
// ... die Summe der Detractors ist ...
var DeS = NPS_0 + NPS_1 + NPS_2 + NPS_3 + NPS_4 + NPS_5 + NPS_6;
// ... die Summe der Passiversist ...
var PaS = NPS_7 + NPS_8;
// ... die Summe der Promoters ist ...
var PrS = NPS_9 + NPS_10;
//sende das ganze wieder zurück zum Player soll der schauen was er damit macht ... du hast jetzt Feierabend, danke für deine Hilfe ...
player.SetVar("Detractors_Summe",DeS);
player.SetVar("Passives_Summe",PaS);
player.SetVar("Promoters_Summe",PrS);
//sage Axel danke dass es dich ueberhaupt gibt und du hier mitmachen darfst
6 Replies
Are they text entry files? Looks like they are coming in as strings and then being concatenated
Have you tried converting to an integer after import?
Hello Phil
1.) in Articulate they are defined as "Numeric Entry Fields"
2.) I played around defining in java script something like this
var int NPS_0 = player.GetVar("NPS_ist_00");
buit failed :-(
cheers
Axel
I would try something like:
I tried this:
var a = player.GetVar("NPS_ist_00");
var NPS_0 = a;
with same result: The variables are treated as "String" and will be concatenated
Axel
So sorry I should have read what I put. I never finished it off:
Or this may be simpler as you convert on the calculation:
I think this will work
If you are confident they are always integers then you could try:
But eval is considered evil by many. One of these should work, sorry not tested any of them.
Think trhis will be the solution:
var a = player.GetVar("NPS_ist_00");
var NPS_0 = parseInt(a);
Thank you very much
Will give it a try, tomorrow
Axel
This discussion is closed. You can start a new discussion or contact Articulate Support.