jhylands

Stats

Apr 24th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.48 KB | None | 0 0
  1. <h1>Stats calculator</h1>
  2. <textarea  id="datas" rows="5" cols="50">1,2,3,4,7,4</textarea>
  3. <br />
  4. <input type="button" onclick="splitdata()" value="Sort shit"/>
  5. <x id="Report"></x>
  6. <script>
  7. var x;
  8. function splitdata(){
  9. var Zx = document.getElementById("datas").value;
  10. x = Zx.split(",");
  11. var n = x.length;
  12. for(i=0;i<n;i++){
  13. x[i] = Number(x[i]);
  14. }
  15. bubblesort();
  16. //start building the report
  17. var report="<p>The data in assending order: " + x.join();
  18. report= report + "<br />The sum is: " + sumx() + " The total data points is: " + n + " Thus the mean is: " + sumx()/n;
  19. report = report + "<br />The lower quatile is: " + x[Math.round(n/4)-1] + " The median is: " + median() + " The upper quatile is: " + x[Math.round(3*n/4)-1];
  20. report = report + "<br />The overall range is: " + (x[n-1]-x[0]) + " The interquatile range is: " + (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  21. report = report + "<br />The lower outliers are:" + Loutliers(); + " The upper outliers are: " + Uoutliers();
  22. report = report + "<br />The mean calculated without outliers is: " + MEAN();
  23.  
  24. report = report + "</p>"
  25. document.getElementById("Report").innerHTML = report;
  26. }
  27. function MEAN(){
  28. var sum = 0;
  29. var n = 0;
  30. for (i=0;i<x.length;i++){
  31. if(x[i]>= GetLowQ() && x[i] <= GetUpQ())
  32. sum = sum + x[i];
  33. n++;
  34. }
  35. }
  36. return sum/n;
  37. }
  38. function Uoutliers(){
  39. var list = "";
  40. for (i=0;i<x.length;i++){
  41. if(x[i]>GetUpQ()){
  42.  list = list + x[i] + ",";
  43.  }
  44. }
  45. if (list==""){
  46. return "No higher outliers!";
  47. }else{
  48. return list;
  49. }
  50. }
  51. function Loutliers(){
  52. var list = "";
  53. for (i=0;i<x.length;i++){
  54. if(x[i]<GetLowQ()){
  55. list = list + x[i] + ",";
  56. }
  57. }
  58. if (list==""){
  59. return "No lower outliers!";
  60. }else{
  61. return list;
  62. }
  63. }
  64. function GetLowQ(){
  65. var n = x.length;
  66. var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  67. var LB = x[Math.round(n/4)-1] - rang;
  68. return LB;
  69. }
  70. function GetUpQ(){
  71. var n = x.length;
  72. var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
  73. var HB = x[Math.round(3*n/4)-1] + rang;
  74. return HB;
  75. }
  76. function median(){
  77. n=x.length;
  78. if (n % 2 == 0){
  79. return (x[n/2]+x[n/2+1])/2;
  80. }else{
  81. return x[n/2+0.5];
  82. }
  83. }//end of median
  84. function sumx(){
  85. var sum = 0;
  86. for (i=0;i<x.length;i++){
  87. sum = sum + x[i];
  88. }
  89. return sum
  90. }
  91. function bubblesort(){
  92. var cat = 0;
  93. while(cat==0){
  94. cat=1;
  95. for (i=0;i<(x.length-1);i++){
  96.  if (x[i]>x[i+1]){//if their in the wrong order swap them
  97.    hold=x[i];
  98.    x[i]=x[i+1];
  99.    x[i+1] = hold;
  100.    cat=0;
  101.   }//exit if
  102.  }//exit the for loop
  103. }//exit bubble sort loop
  104. document.getElementById("datas").value = x.join();
  105. }//exit bubble sort function
  106.  
  107.  
  108. </script>
Advertisement
Add Comment
Please, Sign In to add comment