469,926 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

cannot pass javascript array elements to a calculation that follows

I would like the following code to do a calculation based on the results of a for(var i...etc loop. I cannot 'feed ' the array results into the calculation and keep on getting NaN. Can someone help please.
Thank you



Expand|Select|Wrap|Line Numbers
  1.   function doCalculation ()    {
  2.   for (var i=0; i<=10; i++)    {
  3.     var x=[i];
  4.     var y=[x*Math.exp(3/8).toFixed(2)];
  5.     document.writeln('<p>x=&nbsp;'+x +'&nbsp;y=&nbsp;' +y +'</p>');//this gives results but the next won't compute
  6.     var G= (((x[9]-x[8])*(y[9]-y[8]))+((x[8]-x[7])*(y[8]-y[7]))+((x[7]-x[6])*(y[7]-y[6]))+((x[6]-x[5])*(y[6]-y[5]))+((x[5]-x[4])*(y[5]-y[4]))+((x[4]-x[3])*(y[4]-y[3]))+((x[3]-x[2])*(y[3]-y[2]))+((x[2]-x[1])*(y[2]-y[1]))+((x[1]-x[0])*(y[1]-y[0])));
  7.     document.writeln('G = &nbsp;'+G);
  8.     }
  9.   }
  10.  
Oct 13 '10 #1
8 1310
JKing
1,206 Expert 1GB
Hi Peter.

What is it you are trying to accomplish here? You haven't declared any arrays. Are you trying to fill an array with values and then do a calculation?

Here is an example of an array and looping through it to print.
Expand|Select|Wrap|Line Numbers
  1. var names = new Array();
  2. names[0] = "Lisa";
  3. names[1] = "Bart";
  4. names[2] = "Maggie";
  5.  
  6. for (var i=0;i<3; i++)
  7. {
  8.    document.write(names[i]);
  9. }
  10. //This will print Lisa Bart Maggie
  11.  
Oct 13 '10 #2
I am trying to use the values generated by the loop in the calculation that subtracts , in order, the lower value from the higher the precedes it in the list for x and y, multiplies these two and then adds the next product of the next two pairs as described in the line after the document-writeln. Help with getting these values correctly into one or two arrays ( x and y array) and then correctly identifying which is x10, x 9 and so on is what I cannot get my head around. I hope that this is clear. I don't seem to be able to describe my problem very well!
Oct 13 '10 #3
JKing
1,206 Expert 1GB
Okay, I think this is what you are looking for.

Expand|Select|Wrap|Line Numbers
  1. function doCalculation ()    
  2. {
  3.     //Declare Array Variables
  4.     var x = new Array();
  5.     var y = new Array();
  6.  
  7.     //Loop from 1 to 10 to fill x and y
  8.     for (var i=0; i<=10; i++)    
  9.     {
  10.         x[i] = i;
  11.         y[i]=x[i]*Math.exp(3/8).toFixed(2);
  12.         document.writeln('<p>x=&nbsp;'+x +'&nbsp;y=&nbsp;' +y +'</p>');//this gives results but the next won't compute
  13.     }
  14.  
  15.     //Use the filled arrays in calculation
  16.     var G= (((x[9]-x[8])*(y[9]-y[8]))+((x[8]-x[7])*(y[8]-y[7]))+((x[7]-x[6])*(y[7]-y[6]))+((x[6]-x[5])*(y[6]-y[5]))+((x[5]-x[4])*(y[5]-y[4]))+((x[4]-x[3])*(y[4]-y[3]))+((x[3]-x[2])*(y[3]-y[2]))+((x[2]-x[1])*(y[2]-y[1]))+((x[1]-x[0])*(y[1]-y[0])));
  17.     document.writeln('G = &nbsp;'+G);
  18. }
  19.  
Oct 13 '10 #4
thank you so much. I will try this with the calculation I am actually trying to do and see whether I can make it work. Once again it is to get the x and y values into the subsequent calculations as in this example. I'll give it a go, but will appreciate your help, once again.
Thank you once again!!
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.     var FPF=[];
  4.     var TPF =[];
  5.     var xArray=[];
  6.     var yArray=[];
  7.     var x=2;
  8.     //parseFloat($('#display').val());//1;//slider_form.elements["display"].value;//slider_form.elements.display.value;// slider value
  9.     var LT = parseFloat(-3 + (0.8571 *x));//changes slider value to the left tail value 'with disease'
  10.     var RT= 3;//value for right tail of 'without disease'
  11.     var c= parseFloat(1.739 + (0.6116*x)+(0.01297*x*Math.exp(2))-(0.01142*x*Math.exp(3))-(0.004572*x*Math.exp(4))); //intersect between two curves
  12.  
  13. //from Stephen Whitney application 25yearsofprogramming.com 'probability.js'
  14.  function NormalDensityZx(x, Mean, StdDev)
  15. {
  16.     var a = x - Mean;
  17.     return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev); 
  18. }
  19.  
  20. //----------------------------------------------------------------------------------------------
  21. // Calculates Q(x), the right tail area under the Standard Normal Curve. 
  22. function StandardNormalQx(x)
  23. {
  24.     x = i;
  25.     if(x === 0)    // no approximation necessary for 0
  26.         return 0.50;
  27.  
  28.     var t1,t2,t3,t4,t5,qx;
  29.     var negative = false;
  30.     if(x < 0)
  31.     {
  32.         x = -x;
  33.         negative = true;
  34.     }
  35.     t1 = 1/(1 + (0.2316419 * x)); 
  36.     t2 = t1 * t1; 
  37.     t3 = t2 * t1; 
  38.     t4 = t3 * t1; 
  39.     t5 = t4 * t1;
  40.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  41.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  42.     if(negative == true)
  43.         qx = 1 - qx;
  44.     return qx;
  45. }
  46.  
  47.  
  48. //----------------------------------------------------------------------------------------------
  49. // Calculates P(x), the left tail area under the Standard Normal Curve, which is 1 - Q(x). 
  50. function StandardNormalPx(x)
  51. {
  52.  
  53.  
  54.     x = -2.8+(i-LT);
  55.     if(x === 0)    // no approximation necessary for 0
  56.         return 0.50;
  57.  
  58.     var t1,t2,t3,t4,t5,qx;
  59.     var negative = false;
  60.     if(x < 0)
  61.     {
  62.         x = -x;
  63.         negative = true;
  64.     }
  65.     t1 = 1/(1 + (0.2316419 * x)); 
  66.     t2 = t1 * t1; 
  67.     t3 = t2 * t1; 
  68.     t4 = t3 * t1; 
  69.     t5 = t4 * t1;
  70.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  71.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  72.     if(negative == true)
  73.         qx = 1 - qx;
  74.  
  75.  
  76.     return 1 - qx;
  77.  
  78. }
  79. //end Stephen Whitney probability.js
  80.  
  81. //calculate points for ROC plot from -3 to +3 at unit intervals    
  82. for (var i =-3; i<3.1; i+=0.2 ){
  83.  
  84. if (i<LT){
  85.  
  86.             PQAQX = (StandardNormalQx(x).toFixed(2)); //AUC false positives based on AUC = 1
  87.             FPF =parseFloat(StandardNormalQx(x).toFixed(2));
  88.             TPF = 1;}
  89.  
  90. else if (i>LT && i<RT){
  91.  
  92.  
  93.             PQAQX= (StandardNormalQx(x).toFixed(2));
  94.             PQAPX= (StandardNormalPx(x).toFixed(2));
  95.  
  96.             TPF = (1-StandardNormalPx(x).toFixed(2));
  97.             FPF = parseFloat(StandardNormalQx(x).toFixed(2));
  98. }
  99.  
  100.  
  101. else if (i >3.01){
  102.  
  103.             PQAPX= (StandardNormalPx(x).toFixed(2));
  104.             TPF = (1-StandardNormalPx(x).toFixed(2));
  105.             FPF = 1;
  106.         }
  107.         var xArray=[FPF.toFixed(2)];
  108.         var yArray=[TPF.toFixed(2)];
  109.  
  110.         xArray.push([FPF]);
  111.         yArray.push([TPF]);
  112.         document.writeln('x:&nbsp;'+(xArray[0])+',&nbsp;y:&nbsp;'+(yArray[0])+'<BR>');
  113.         G1=1-((xArray[0]-xArray[1])*(yArray[0]-yArray[1])+(xArray[1]-xArray[2])*(yArray[1]-yArray[2])+(xArray[2]-xArray[3])*(yArray[2]-yArray[3])+(xArray[3]-xArray[4])*(yArray[3]-yArray[4])+(xArray[4]-xArray[5])*(yArray[4]-yArray[5])+(xArray[5]-xArray[6])*(yArray[5]-yArray[6])+(xArray[6]-xArray[7])*(yArray[6]-yArray[7])+(xArray[7]-xArray[8])*(yArray[7]-yArray[8])+(xArray[8]-xArray[9])*(yArray[8]-yArray[9])+(xArray[9]-xArray[10])*(yArray[9]-yArray[10])+(xArray[10]-xArray[11])*(yArray[10]-yArray[11])+(xArray[11]-xArray[12])*(yArray[11]-yArray[12])+(xArray[12]-xArray[13])*(yArray[12]-yArray[13])+(xArray[13]-xArray[14])*(yArray[13]-yArray[14])+(xArray[15]-xArray[16])*(yArray[15]-yArray[16])+(xArray[16]-xArray[17])*(yArray[16]-yArray[17])+(xArray[18]-xArray[19])*(yArray[18]-yArray[19])+(xArray[19]-xArray[20])*(yArray[19]-yArray[20])+(xArray[20]-xArray[21])*(yArray[20]-yArray[21])+(xArray[21]-xArray[22])*(yArray[21]-yArray[22])+(xArray[22]-xArray[23])*(yArray[22]-yArray[23])+(xArray[23]-xArray[24])*(yArray[23]-yArray[24])+(xArray[24]-xArray[25])*(yArray[24]-yArray[25])+(xArray[25]-xArray[26])*(yArray[25]-yArray[26])+(xArray[26]-xArray[27])*(yArray[26]-yArray[27])+(xArray[27]-xArray[28])*(yArray[27]-yArray[28])+(xArray[28]-xArray[29])*(yArray[28]-yArray[29])+(xArray[29]-xArray[30])*(yArray[29]-yArray[30]));
  114.         var AUC= ((G1 + 1)/2);
  115.         document.writeln('area under ROC curve ='+ AUC);
  116.         }
  117.         //calculate area under the curve
  118.         //take x and y values from function and do AUC calculation
  119.         //var G1= 1-Math.sum((x1-x2)*(y1-y2)+...); this function does not exist! description only
  120.         var AUC= (G1 + 1)/2;
  121.         //var d=AUC-0.5;
  122.  
  123.         </script>
  124.  
Oct 13 '10 #5
JKing
1,206 Expert 1GB
I see a few problems.

Your for loop will never enter into this if block.
You increment your for loop by 0.2 and terminate when i < 3.1. i will equal 3.0 on its last iteration.
Expand|Select|Wrap|Line Numbers
  1. else if (i >3.01){
  2.  
  3.             PQAPX= (StandardNormalPx(x).toFixed(2));
  4.             TPF = (1-StandardNormalPx(x).toFixed(2));
  5.             FPF = 1;
  6.         }
  7.  
The following lines in your for loop means that every iteration of the for loop you are creating an array variable with a single index and a single value. You then add FPF to xArray and TPF to yArray. This means both of these arrays never have more than 2 values.

Expand|Select|Wrap|Line Numbers
  1. var xArray=[FPF.toFixed(2)];
  2. var yArray=[TPF.toFixed(2)];
  3.  
  4. xArray.push([FPF]);
  5. yArray.push([TPF]);
  6.  
Now when you go to output all of this and attempt to do your calculation it will fail because you trying to access an index of the array that doesn't exist.

I rearranged things for you once again.
Expand|Select|Wrap|Line Numbers
  1.     <script type="text/javascript">
  2.  
  3.     var FPF=[];
  4.     var TPF =[];
  5.     var xArray=[];
  6.     var yArray=[];
  7.     var x=2;
  8.     //parseFloat($('#display').val());//1;//slider_form.elements["display"].value;//slider_form.elements.display.value;// slider value
  9.     var LT = parseFloat(-3 + (0.8571 *x));//changes slider value to the left tail value 'with disease'
  10.     var RT= 3;//value for right tail of 'without disease'
  11.     var c= parseFloat(1.739 + (0.6116*x)+(0.01297*x*Math.exp(2))-(0.01142*x*Math.exp(3))-(0.004572*x*Math.exp(4))); //intersect between two curves
  12.  
  13. //from Stephen Whitney application 25yearsofprogramming.com 'probability.js'
  14.  function NormalDensityZx(x, Mean, StdDev)
  15. {
  16.     var a = x - Mean;
  17.     return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev); 
  18. }
  19.  
  20. //----------------------------------------------------------------------------------------------
  21. // Calculates Q(x), the right tail area under the Standard Normal Curve. 
  22. function StandardNormalQx(x)
  23. {
  24.     x = i;
  25.     if(x === 0)    // no approximation necessary for 0
  26.         return 0.50;
  27.  
  28.     var t1,t2,t3,t4,t5,qx;
  29.     var negative = false;
  30.     if(x < 0)
  31.     {
  32.         x = -x;
  33.         negative = true;
  34.     }
  35.     t1 = 1/(1 + (0.2316419 * x)); 
  36.     t2 = t1 * t1; 
  37.     t3 = t2 * t1; 
  38.     t4 = t3 * t1; 
  39.     t5 = t4 * t1;
  40.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  41.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  42.     if(negative == true)
  43.         qx = 1 - qx;
  44.     return qx;
  45. }
  46.  
  47.  
  48. //----------------------------------------------------------------------------------------------
  49. // Calculates P(x), the left tail area under the Standard Normal Curve, which is 1 - Q(x). 
  50. function StandardNormalPx(x)
  51. {
  52.  
  53.  
  54.     x = -2.8+(i-LT);
  55.     if(x === 0)    // no approximation necessary for 0
  56.         return 0.50;
  57.  
  58.     var t1,t2,t3,t4,t5,qx;
  59.     var negative = false;
  60.     if(x < 0)
  61.     {
  62.         x = -x;
  63.         negative = true;
  64.     }
  65.     t1 = 1/(1 + (0.2316419 * x)); 
  66.     t2 = t1 * t1; 
  67.     t3 = t2 * t1; 
  68.     t4 = t3 * t1; 
  69.     t5 = t4 * t1;
  70.     qx = NormalDensityZx(x,0,1) * ((0.319381530 * t1) + (-0.356563782 * t2) + 
  71.                                 (1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
  72.     if(negative == true)
  73.         qx = 1 - qx;
  74.  
  75.  
  76.     return 1 - qx;
  77.  
  78. }
  79. //end Stephen Whitney probability.js
  80.  
  81.  
  82. var xArray=[];
  83. var yArray=[];
  84.  
  85. //calculate points for ROC plot from -3 to +3 at unit intervals    
  86. for (var i =-3; i<3.1; i+=0.2 ){
  87.  
  88.     if (i<LT){
  89.  
  90.                 PQAQX = (StandardNormalQx(x).toFixed(2)); //AUC false positives based on AUC = 1
  91.                 FPF =parseFloat(StandardNormalQx(x).toFixed(2));
  92.                 TPF = 1;
  93.     }
  94.  
  95.     else if (i>LT && i<RT){
  96.  
  97.  
  98.             PQAQX= (StandardNormalQx(x).toFixed(2));
  99.             PQAPX= (StandardNormalPx(x).toFixed(2));
  100.  
  101.             TPF = (1-StandardNormalPx(x).toFixed(2));
  102.             FPF = parseFloat(StandardNormalQx(x).toFixed(2));
  103. }
  104.  
  105.  
  106. else if (i >3.01){
  107.  
  108.             PQAPX= (StandardNormalPx(x).toFixed(2));
  109.             TPF = (1-StandardNormalPx(x).toFixed(2));
  110.             FPF = 1;
  111.         }
  112.  
  113.  
  114.         xArray.push(FPF);
  115.         yArray.push(TPF);
  116.  
  117.         }
  118.         //calculate area under the curve
  119.         //take x and y values from function and do AUC calculation
  120.         //var G1= 1-Math.sum((x1-x2)*(y1-y2)+...); this function does not exist! description only
  121.         document.writeln('x:&nbsp;'+(xArray[0])+',&nbsp;y:&nbsp;'+(yArray[0])+'<BR>');
  122.         var G1=1-((xArray[0]-xArray[1])*(yArray[0]-yArray[1])+(xArray[1]-xArray[2])*(yArray[1]-yArray[2])+(xArray[2]-xArray[3])*(yArray[2]-yArray[3])+(xArray[3]-xArray[4])*(yArray[3]-yArray[4])+(xArray[4]-xArray[5])*(yArray[4]-yArray[5])+(xArray[5]-xArray[6])*(yArray[5]-yArray[6])+(xArray[6]-xArray[7])*(yArray[6]-yArray[7])+(xArray[7]-xArray[8])*(yArray[7]-yArray[8])+(xArray[8]-xArray[9])*(yArray[8]-yArray[9])+(xArray[9]-xArray[10])*(yArray[9]-yArray[10])+(xArray[10]-xArray[11])*(yArray[10]-yArray[11])+(xArray[11]-xArray[12])*(yArray[11]-yArray[12])+(xArray[12]-xArray[13])*(yArray[12]-yArray[13])+(xArray[13]-xArray[14])*(yArray[13]-yArray[14])+(xArray[15]-xArray[16])*(yArray[15]-yArray[16])+(xArray[16]-xArray[17])*(yArray[16]-yArray[17])+(xArray[18]-xArray[19])*(yArray[18]-yArray[19])+(xArray[19]-xArray[20])*(yArray[19]-yArray[20])+(xArray[20]-xArray[21])*(yArray[20]-yArray[21])+(xArray[21]-xArray[22])*(yArray[21]-yArray[22])+(xArray[22]-xArray[23])*(yArray[22]-yArray[23])+(xArray[23]-xArray[24])*(yArray[23]-yArray[24])+(xArray[24]-xArray[25])*(yArray[24]-yArray[25])+(xArray[25]-xArray[26])*(yArray[25]-yArray[26])+(xArray[26]-xArray[27])*(yArray[26]-yArray[27])+(xArray[27]-xArray[28])*(yArray[27]-yArray[28])+(xArray[28]-xArray[29])*(yArray[28]-yArray[29])+(xArray[29]-xArray[30])*(yArray[29]-yArray[30]));
  123.         var AUC= ((G1 + 1)/2);
  124.         document.writeln('G1: ' + G1 + '<br />');
  125.         document.writeln('area under ROC curve ='+ AUC);
  126.         //var d=AUC-0.5;
  127.  
  128.     </script>
  129.  
Oct 13 '10 #6
Thank you so much. You have no idea how long I have wrestled with this. I shall go through this carefully to understand how one ought to order things
Oct 14 '10 #7
Hi there,
Weird behaviour of the calculation. Changing the x value changes the AUC, however at -3 the AUC should be 0.5 , but any value over or below 0 gives an AUC approaching 1, whilst at 0, the AUC gives 0.47 which is probably a flaw in the values in the calculation since I have used a 5th power polynomial instead of 6 or 7. Do your kingly powers stretch to resolving this? Basically in the actual application, there is a slider which changes the shape of a plot and consequently the AUC. I could send the whole thing to you except that it is quite large and uses loads of add-ins from jqplot - probably best sent as a zipped file straight to you. Please don't hesitate to tell me that I am asking too much! The whole thing is actually to allow medical students and doctors to understand the relativity of numbers in biology and diagnosis and will from part of a teaching module on medical arithmetic.
Oct 14 '10 #8
PS I am acknowledging your great help with the following:
Help with javascript J. King of Ontario Canada via www.bytes.com
Would you prefer something else?
Oct 14 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by RyanTaylor | last post: by
14 posts views Thread by Abhi | last post: by
Xx r3negade
3 posts views Thread by Xx r3negade | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.