I created a piece of JavaScript to run within an eLearning module that I built with Articulate Storyline 2 (eLearning rapid development software). The code is meant to work as a calculator, outputting how much it would cost a company to run inductions for new staff.

The user inputs the numbers into the Storyline player/elearning module in their browser. The code retrieves these inputs and stores it in variables. This information is used to do the calculations and the user gets the result on the final screen in the browser.

Oddly, the JavaScript runs without issue locally, but when it is uploaded to our server, the result always comes back as zero. I'm not sure why this is happening.

Also, the numbers are always whole numbers. They never come back as something like £13,553.99. It usually rounds it to the nearest whole number, e.g. £13,554.00.

If anyone could give me some advice, I'd really appreciate it. I've included the code and comments so you can see what I've done. If you need more information, please let me know.

// Retrieve variables from Storyline file.
var player=GetPlayer();

// Assign Storyline variables to JS variables. Each letter represents a data entry box in the elearning module.
var aa = player.GetVar("a");
var bb = player.GetVar("b"); 
var cc = player.GetVar("c");  
var dd = player.GetVar("d"); 
var ee = player.GetVar("e");
var ff = player.GetVar("f"); 
var gg = player.GetVar("g");  
var hh = player.GetVar("h"); 
var ii = player.GetVar("i");
var jj = player.GetVar("j"); 
var ll = player.GetVar("l");  
var qq = player.GetVar("q");  

// Convert strings to integers
var aa = parseInt(aa);
var bb = parseInt(bb);
var cc = parseInt(cc);
var dd = parseInt(dd);
var ee = parseInt(ee);
var ff = parseInt(ff);
var gg = parseInt(gg);
var hh = parseInt(hh);
var ii = parseInt(ii);
var jj = parseInt(jj);
var ll = parseInt(ll);
var qq = parseInt(qq);

// Add comma to thousands.
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

// Use the numbers stored in the variables to do the induction calculations.
// # per month
var mm = aa * 12;

// £ new recruit/hr + £ trainer/hr
var ss = bb + cc; 

// Duration mins
var nn = dd / 60;

// (Room booking admin mins + admin records) / 60 * £ admin/hr
var oo = (ee + gg) / 60 * ff;

// Repeating hrs/month
var pp = jj * (bb + cc);

// # of trainers
var rr = (ll * qq) * (cc + cc); 

//Results calculations
var tt = (ss * nn) + oo; 

var uu = tt + hh + ii;

var vv = uu * mm;

var ww = pp * 12;

// Calculate final result, round to 2 decimal places and convert number to a string. Add comma to define thousands.
var total = vv + ww + rr;
var totalRnd = total.toFixed(2);
var totalStr = totalRnd.toString();
var totalCost = addCommas(totalStr);

// Sends result back to Storyline.
player.SetVar("result",totalCost);

UPDATE: I use parseInt() instead of parseFloat(), which explains the whole numbers issue.

1

There are 1 best solutions below

2
On BEST ANSWER

parseInt parses the input to an integer value. You need to use parseFloat instead. However, it is strange this worked locally for you. I think you had integer values there, so parseInt did not ruin preciseness.