arwoodsnz
08-16-2004, 12:51 PM
Hi,
I am just starting to learn JS and want to have a user enter an unknown quantity of numbers (both positive and negative), then loop through the list/array I have put the numbers into, counting how many are negative, then outputting a message with the enitre list and the count of negative entries.
I have put a button on my page which calls a script runlist() which I thought I would then use prompt to get the numbers and then do a for loop incrementing a counter each time I found it was negative. Then I thought I would use the document.write function to output the message.
Where I am having probelsm are : a) it is real ugly with a prompt to get the numbers, perhaps a table which grows would be more elegant.
b) I have not worked out what parts I want in the header and which in the body.
This probably sounds real easy to you guys.
Due to feedback from Willy (thanks) here is my cirrent code
<input type="button" value="Click me!" name="myButton"
onclick="runlist()"><br>
<script language="JavaScript" type="text/javascript">
function runlist()
{
//
// this function asks the user to enter a list of numbers
//
var list = new Array;
var cntr = 0;
var test = 0;
var counter = 0;
var i = 1;
while (test < 99999)
{
test = prompt("Please enter a number (99999 to stop entering)","Enter Number Here");
if (test == 99999)
{
}
else
{
list[cntr] = test;
cntr++;
}
}
for (i=1; i<cntr;i++)
{
if (list[i] < 0)
{
counter++;
}
}
}
document.write("You Entered : " + list+ "<br>);
document.write("The list had " + counter + " negative values");
}
</script>
I am just starting to learn JS and want to have a user enter an unknown quantity of numbers (both positive and negative), then loop through the list/array I have put the numbers into, counting how many are negative, then outputting a message with the enitre list and the count of negative entries.
I have put a button on my page which calls a script runlist() which I thought I would then use prompt to get the numbers and then do a for loop incrementing a counter each time I found it was negative. Then I thought I would use the document.write function to output the message.
Where I am having probelsm are : a) it is real ugly with a prompt to get the numbers, perhaps a table which grows would be more elegant.
b) I have not worked out what parts I want in the header and which in the body.
This probably sounds real easy to you guys.
Due to feedback from Willy (thanks) here is my cirrent code
<input type="button" value="Click me!" name="myButton"
onclick="runlist()"><br>
<script language="JavaScript" type="text/javascript">
function runlist()
{
//
// this function asks the user to enter a list of numbers
//
var list = new Array;
var cntr = 0;
var test = 0;
var counter = 0;
var i = 1;
while (test < 99999)
{
test = prompt("Please enter a number (99999 to stop entering)","Enter Number Here");
if (test == 99999)
{
}
else
{
list[cntr] = test;
cntr++;
}
}
for (i=1; i<cntr;i++)
{
if (list[i] < 0)
{
counter++;
}
}
}
document.write("You Entered : " + list+ "<br>);
document.write("The list had " + counter + " negative values");
}
</script>