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
Phil Mayor

So sorry I should have read what I put. I never finished it off:

var a = player.GetVar("NPS_ist_00");
var NPS_0 = parseInt(a);

Or this may be simpler as you convert on the calculation:

var DeS = parseFloat(NPS_0) + parseFloat(NPS_1) + parseFloat(NPS_2) + parseFloat(NPS_3) + parseFloat(NPS_4) + parseFloat(NPS_5) + parseFloat(NPS_6);

I think this will work

var DeS = parseFloat(NPS_0 + NPS_1 + NPS_2 + NPS_3 + NPS_4 + NPS_5 + NPS_6);

If you are confident they are always integers then you could try:

var DeS = eval(NPS_0 + NPS_1 + NPS_2 + NPS_3 + NPS_4 + NPS_5 + NPS_6);

But eval is considered evil by many. One of these should work, sorry not tested any of them.

 

This discussion is closed. You can start a new discussion or contact Articulate Support.