Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to Array

Status
Not open for further replies.

asfaw

Programmer
Jun 28, 2004
36
CA
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>
 
alert(data);

output - 5.1,0.1,2.4,1.7,0.5,3.0,0.9,1.8,2.1,0.7

alert(numbers);

output:

5.1,0.1,2.4,1.7,0.5,3.0,0.9,1.8,2.1,0.7

alert(numbers.length);

output:

10

However, the function call to function median(set) returns NaN.

Thank you.

asfaw
 
Looks like it's recognizing the values as strings and not numbers, you should call parseFloat on each value to fill an array of actual numbers.

Cheers,
Dian
 
Hi Dian,

Thank you for the quick response. How do you do use parseFloat.

asfaw
 
Right after your line of code, var numbers = data.split(",");, add:
Code:
for(var i=0; i<numbers.length; i++)
 numbers[i] = parseFloat(numbers[i]);

'hope that helps.

I was playing with the code before and so I know this keeps the code from breaking.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
It works. Thank you so much for your kind help.

asfaw
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top