# 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:

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

I ran the math w/two different values. If pct = 100 the value is returns match; try 90 and see if the math looks better. Square root of (0/n) = 0

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.