PDA

View Full Version : Would it be better to make this entire thing a function

thenextbesthang
10-20-2006, 09:59 PM
or to just write it out as is.

<html>
<title>CSC Lab 9</title>
<body>
<script type = "text/javascript">
var userinput = window.prompt("Enter a set of numbers, separated by spaces", "0 0 1 2 3 4 5 6 7 8 9");
var newarray = userinput.split(" ");
var alength = newarray.length;

function sorting(a, b)
{
return a-b;
}
newarray.sort(sorting);

for(i=0;i<alength;i++)
{
newarray[i] = parseInt(newarray[i]);
}
arsum = 0;
for(i=0;i<alength;i++)
{
arsum+= newarray[i];
}
var mean= (arsum/alength);
var median = 0;
if(alength%2 == 1)
{
median = newarray[parseInt(alength/2)];
}
else
{
var temp1med = newarray[parseInt(alength/2)];
var temp2med = newarray[parseInt((alength/2)-1)];
var median= (temp1med+temp2med)/2
}
var largestfreq = 0;
var largestmode = 0;
var currentfreq = 0;
var currentmode = newarray[0];
for(i=0;i<=alength;i++)
{
if(newarray[i] == currentmode)
{
currentfreq++;
}
else
{
if (currentfreq > largestfreq)
{
largestfreq = currentfreq;
largestmode = currentmode;
}
currentmode = newarray[i];
currentfreq = 1;
}
}
if (currentfreq > largestfreq)
{
largestfreq = currentfreq;
largestmode = currentmode;
}