Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently May 21st, 2024, 7:51 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: JavaScript Math Formula
PostPosted: October 10th, 2016, 3:37 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Can't seem to get a result from this JavaScript math formula. Have added it as a JavaScript item to a button in an HTML5 test pub (plus put in some test number values for variables), published as HTML5 to test, but no result.

Must be scripting it incorrectly?

JavaScript (based on math formula found online-see posting: viewtopic.php?f=4&t=4573&p=20872&hilit=probit#p20872- for normalizing statistical data):

Code:
function NORMSINV(p)

{
    var a = new Array(-3.969683028665376e+01,  2.209460984245205e+02,
                      -2.759285104469687e+02,  1.383577518672690e+02,
                      -3.066479806614716e+01,  2.506628277459239e+00);

    var b = new Array(-5.447609879822406e+01,  1.615858368580409e+02,
                      -1.556989798598866e+02,  6.680131188771972e+01,
                      -1.328068155288572e+01 );

    var c = new Array(-7.784894002430293e-03, -3.223964580411365e-01,
                      -2.400758277161838e+00, -2.549732539343734e+00,
                      4.374664141464968e+00,  2.938163982698783e+00);

    var d = new Array (7.784695709041462e-03, 3.224671290700398e-01,
                       2.445134137142996e+00,  3.754408661907416e+00);

   
    var plow  = 0.02425;
    var phigh = 1 - plow;

   
    if ( p < plow ) {
             var q  = Math.sqrt(-2*Math.log(p));
             return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
                                             ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
    }

   
    if ( phigh < p ) {
             var q  = Math.sqrt(-2*Math.log(1-p));
             return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
                                                    ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
    }

   
    var q = p - 0.5;
    var r = q*q;
    return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
                             (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
}

totalscore5 = .85
totalscore6 = .225
totalscore7 = .975
totalscore8 = .125

var p = totalscore5
var probit1 = NORMSINV(p)
var p = totalscore6
var probit2 = NORMSINV(p)
var Dprime1 = probit1-probit2

var p = totalscore8
var probit3 = NORMSINV(p)
var p = totalscore7
var probit4 = NORMSINV(p)
var Dprime2 = probit3-probit4


Have also tried this by declaring the variables first: let's me lose the var prefix (var p = becomes p =).

Code:
var a;
var b;
var c;
var d;
var plow;
var phigh;
var p;
var q;
var r;
var probit1;
var probit2;
var probit3;
var probit4;
var Dprime1;
var Dprime2;

function NORMSINV(p)

{
    a = new Array(-3.969683028665376e+01,  2.209460984245205e+02,
                      -2.759285104469687e+02,  1.383577518672690e+02,
                      -3.066479806614716e+01,  2.506628277459239e+00);

    b = new Array(-5.447609879822406e+01,  1.615858368580409e+02,
                      -1.556989798598866e+02,  6.680131188771972e+01,
                      -1.328068155288572e+01 );

    c = new Array(-7.784894002430293e-03, -3.223964580411365e-01,
                      -2.400758277161838e+00, -2.549732539343734e+00,
                      4.374664141464968e+00,  2.938163982698783e+00);

    d = new Array (7.784695709041462e-03, 3.224671290700398e-01,
                       2.445134137142996e+00,  3.754408661907416e+00);

   
    plow  = 0.02425;
    phigh = 1 - plow;

   
    if ( p < plow ) {
             q  = Math.sqrt(-2*Math.log(p));
             return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
                                             ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
    }

   
    if ( phigh < p ) {
             q  = Math.sqrt(-2*Math.log(1-p));
             return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
                                                    ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
    }

   
    q = p - 0.5;
    r = q*q;
    return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
                             (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
}

//totalscore5 = .85
//totalscore6 = .225
//totalscore7 = .975
//totalscore8 = .125

p = totalscore5
probit1 = NORMSINV(p)
p = totalscore6
probit2 = NORMSINV(p)
Dprime1 = probit1-probit2

p = totalscore8
probit3 = NORMSINV(p)
p = totalscore7
probit4 = NORMSINV(p)


Still didn't work. Can't get Dprime1 and Dprime2 as results which need to display in textboxes.

Any help appreciated.

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: JavaScript Math Formula
PostPosted: October 10th, 2016, 9:57 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
If you are trying to get the result of a Javascript function into an Opus variable then you need to follow the method in this video:

https://www.youtube.com/watch?v=tEHrDlY ... 4ucrELVYhq

<mack>

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: JavaScript Math Formula
PostPosted: October 11th, 2016, 12:17 am 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Thanks for this. Watched the video: learned much.

Realized that we had worked on a similar solution in slightly different form last year: viewtopic.php?f=22&t=5603&hilit=totalscore. :oops:

So, I think that revisiting it will again yield the solution for this newer project.

_________________
Stephen


For this message Stephen has been thanked by : mackavi


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group