...

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>
<head>
<title>CSC Lab 9</title>
</head>
<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;
}

document.write("Your mean is '+mean+' <br>");
document.write("Your median is '+median+' <br>");
document.write("Your mode is '+largestmode+' <br>");


</script>
</body>
</html>

_Aerospace_Eng_
10-20-2006, 10:54 PM
Maybe not one big function just seperate functions. I was taught that if I wrote a method that the method (function) should do only one thing and it should do it well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum