Hello all,
For the life of me I can not figure out why the following code does not work. Please note that when I replace
var data = new String(prompt("Please enter sample numbers separated by a comma",""));
var numbers = data.split(",");
with :
var numbers = new Array(5.1,0.1,2.4,1.7,0.5,3.0,0.9,1.8,2.1,0.7)
it works. Your help is very much appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
var data = new String(prompt("Please enter sample numbers separated by a comma",""));
var numbers = data.split(",");
var firstSet = new Array();
var secondSet = new Array();
var thirdSet = new Array();
// numbers = numbers.sort(sortNumber);
thirdSet = numbers.reverse();
var first = null;
var second = null;
var third = null;
if (first == null) {
first = median(numbers);
alert(first);
}
if (second == null) {
for (i=0; numbers < first; i++) {
firstSet = numbers;
}
second = median(firstSet);
alert(second);
}
if (third == null) {
thirdSet = thirdSet.reverse();
for (i=0; first < thirdSet; i++) {
secondSet = thirdSet;
}
third = median(secondSet);
alert(third);
}
function sortNumber(a, b)
{
return a - b;
}
function median(set) {
var values = new Array(set);
values = set.sort(sortNumber);
var valuesLength = values.length;
if(valuesLength%2 !=0) {
var x = 0;
x = values[Math.floor(valuesLength/2)]
// return(values[Math.floor(valuesLength/2)]);
return x;
}
else {
var half = valuesLength/2;
return((values[half] + values[half - 1])/2);
}
}
</script>
</body>
</html>
For the life of me I can not figure out why the following code does not work. Please note that when I replace
var data = new String(prompt("Please enter sample numbers separated by a comma",""));
var numbers = data.split(",");
with :
var numbers = new Array(5.1,0.1,2.4,1.7,0.5,3.0,0.9,1.8,2.1,0.7)
it works. Your help is very much appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
var data = new String(prompt("Please enter sample numbers separated by a comma",""));
var numbers = data.split(",");
var firstSet = new Array();
var secondSet = new Array();
var thirdSet = new Array();
// numbers = numbers.sort(sortNumber);
thirdSet = numbers.reverse();
var first = null;
var second = null;
var third = null;
if (first == null) {
first = median(numbers);
alert(first);
}
if (second == null) {
for (i=0; numbers < first; i++) {
firstSet = numbers;
}
second = median(firstSet);
alert(second);
}
if (third == null) {
thirdSet = thirdSet.reverse();
for (i=0; first < thirdSet; i++) {
secondSet = thirdSet;
}
third = median(secondSet);
alert(third);
}
function sortNumber(a, b)
{
return a - b;
}
function median(set) {
var values = new Array(set);
values = set.sort(sortNumber);
var valuesLength = values.length;
if(valuesLength%2 !=0) {
var x = 0;
x = values[Math.floor(valuesLength/2)]
// return(values[Math.floor(valuesLength/2)]);
return x;
}
else {
var half = valuesLength/2;
return((values[half] + values[half - 1])/2);
}
}
</script>
</body>
</html>