elresleff
11-19-2010, 04:31 AM
I am new to learning JS and am trying to create an array through a prompt. It seems to work, but I believe it is treating the prompted numbers as strings not numbers. I am parsing the negatives and zeros, and positives and counting them. It doesn't recognize the negative sign.
Here's my js:
function counter()
{
var numArr = new Array(Number(prompt("Please enter and array of numbers, in any order, separated by a comma..." + '\n' +
"In the following format: -2,0,2")));
var positives = 0;
var negatives = 0;
var zeros = 0;
for (var i = 0; i < numArr.length; i++)
{
switch (true) {
case numArr[i] < 0:
negatives++;
break;
case numArr[i] == 0:
zeros++;
break;
case numArr[i] > 0:
positives++;
break;
}
}
alert("You entered:"+ '\n' +
"Number of Negatives: " + negatives + '\n' +
"Number of Zeros: " + zeros + '\n' +
"Number of Positives: " + positives);
}
counter();
Any suggestions?
Here's my js:
function counter()
{
var numArr = new Array(Number(prompt("Please enter and array of numbers, in any order, separated by a comma..." + '\n' +
"In the following format: -2,0,2")));
var positives = 0;
var negatives = 0;
var zeros = 0;
for (var i = 0; i < numArr.length; i++)
{
switch (true) {
case numArr[i] < 0:
negatives++;
break;
case numArr[i] == 0:
zeros++;
break;
case numArr[i] > 0:
positives++;
break;
}
}
alert("You entered:"+ '\n' +
"Number of Negatives: " + negatives + '\n' +
"Number of Zeros: " + zeros + '\n' +
"Number of Positives: " + positives);
}
counter();
Any suggestions?