Help with javascript calcuation

Apr 12, 2021

This is a highly niche request but I am hoping that someone who knows more about javascript that I do can help me with a calculator I'm trying to create for calculating 95% confidence intervals.

Here's the calculator I've made so far. It's supposed to enable users to input their own figures for pct and n, then generate the range of confidence interval for them.

I am using javascript to calculate the confidence interval.

The formula for calculating the 95% confidence interval is as follows:

Illustration of the formula for calculating 95% confidence interval

My slide/trigger setup is as follows:

 

and the javascript I'm using is as follows:

var player = GetPlayer();
var a = player.GetVar("pct");
var b = player.GetVar("n");
x = a-(1.96*(Math.sqrt((a*(100-a)/b))));
y = a+(1.96*(Math.sqrt((a*(100-a)/b))));
player.SetVar("CILower",x);
player.SetVar("CIUpper",y);

The calculator generates the correct figure for CILower, but for some reason that I can't fathom the CIUpper figure isn't right. 

Can anyone see what I'm doing wrong?

 

3 Replies
Xavier Toyos

Hi Jade,

I think I have discovered what happens to your Javascript. Here is the correct Javascript...

var player = GetPlayer();
var a = player.GetVar("pct");
var b = player.GetVar("n");
a = Number.parseFloat(a);
b = Number.parseFloat(b);
x = a-(1.96*(Math.sqrt((a*(100-a)/b))));;
y = a+(1.96*(Math.sqrt((a*(100-a)/b))));;
player.SetVar("CILower",x);
player.SetVar("CIUpper",y);

You do nothing wrong, the variable becomes a string and you have to convert the string into a variable with the Number.parseFloat() function. If you don't want decimals, use the ParseInt() function.

Hope this can help you