Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <h1>Stats calculator</h1>
- <textarea id="datas" rows="5" cols="50">1,2,3,4,7,4</textarea>
- <br />
- <input type="button" onclick="splitdata()" value="Sort shit"/>
- <x id="Report"></x>
- <script>
- //basic analysis of one set of data through various methods
- var x;
- function splitdata(){
- var Zx = document.getElementById("datas").value;
- x = Zx.split(",");
- var n = x.length;
- for(i=0;i<n;i++){
- x[i] = Number(x[i]);
- }
- bubblesort();
- //start building the report
- var report="<p>The data in ascending order: " + x.join();
- report= report + "<br />The sum is: " + sumx() + " The total data points is: " + n + " Thus the mean is: " + sumx()/n;
- report = report + "<br />The lower quartile is: " + x[Math.round(n/4)-1] + " The median is: " + median() + " The upper quartile is: " + x[Math.round(3*n/4)-1];
- report = report + "<br />The overall range is: " + (x[n-1]-x[0]) + " The interquartile range is: " + (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
- report = report + "<br />The lower outliers are:" + Loutliers() + " The upper outliers are: " + Uoutliers();
- report = report + "<br />The mean calculated without outliers is: " + MEAN();
- report = report + "<br />The sum of the squares is: " + sumx2();
- report = report + "<br />The variance is: " + variance() + " Hence the standard deviation is: " + Math.pow(variance(),0.5);
- report = report + "</p>"
- document.getElementById("Report").innerHTML = report;
- }
- function variance(){
- var n = x.length;
- return sumx2()/n - Math.pow(sumx()/n,2);
- }
- function sumx2(){
- var sum = 0;
- for (i=0;i<x.length;i++){
- sum = sum + Math.pow(x[i],2);
- }
- return sum;
- }
- function MEAN(){
- var sum = 0;
- var n = 0;
- for (i=0;i<x.length;i++){
- if(x[i]>= GetLowQ() && x[i] <= GetUpQ()){
- sum = sum + x[i];
- n++;
- }
- }
- return sum/n;
- }
- function Uoutliers(){
- var list = "";
- for (i=0;i<x.length;i++){
- if(x[i]>GetUpQ()){
- list = list + x[i] + ",";
- }
- }
- if (list==""){
- return "No higher outliers!";
- }else{
- return list;
- }
- }
- function Loutliers(){
- var list = "";
- for (i=0;i<x.length;i++){
- if(x[i]<GetLowQ()){
- list = list + x[i] + ",";
- }
- }
- if (list==""){
- return "No lower outliers!";
- }else{
- return list;
- }
- }
- function GetLowQ(){
- var n = x.length;
- var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
- var LB = x[Math.round(n/4)-1] - rang;
- return LB;
- }
- function GetUpQ(){
- var n = x.length;
- var rang = 1.5 * (x[Math.round(3*n/4)-1] - x[Math.round(n/4)-1]);
- var HB = x[Math.round(3*n/4)-1] + rang;
- return HB;
- }
- function median(){
- n=x.length;
- alert(n % 2);
- if (n % 2 == 0){
- return (x[n/2]+x[n/2-1])/2;
- }else{
- return x[n/2+0.5];
- }
- }//end of median
- function sumx(){
- var sum = 0;
- for (i=0;i<x.length;i++){
- sum = sum + x[i];
- }
- return sum;
- }
- function bubblesort(){
- var cat = 0;
- while(cat==0){
- cat=1;
- for (i=0;i<(x.length-1);i++){
- if (x[i]>x[i+1]){//if their in the wrong order swap them
- hold=x[i];
- x[i]=x[i+1];
- x[i+1] = hold;
- cat=0;
- }//exit if
- }//exit the for loop
- }//exit bubble sort loop
- document.getElementById("datas").value = x.join();
- }//exit bubble sort function
- </script>
Advertisement
Add Comment
Please, Sign In to add comment