PDA

View Full Version : For statement with counter using variables


Lisawynn
03-02-2004, 09:32 PM
fyi - newbie
I need to create a page that accepts numbers entered by a user - a starting number, an ending number and a number to increment by - and onclick of a button, displays the information on a new page. The user can enter either positive or negative numbers. If the starting and ending numbers are the same, the user is alerted that their number selections are invalid. The part I am having difficulty with is getting the text box values submitted by the user to be used for the counter value. What it looks like to me is that instead of converting the variable "a" for example to a number, it is concatenating the string and just adding the value of text6 together i.e.5+5+5+5 to come up with 555555. The script goes on forever and locks your machine so don't run it. The following is what I currently have:


<html>
<script language="javascript">

function displaynumbers(){
var a
a=document.form4.text4.value
var b
b=document.form4.text5.value
var c
c=document.form4.text6.value
if(document.form4.text4.value==document.form4.text5.value)
{
alert("Your number selections are invalid.")
}
for(counter=a;counter<b;counter=counter+c)
{
document.write((counter+"<br/>"))

}
}
</script>
<body>
<form name=form4>
<p align=center>10 marks</p>
1.&nbspHave the user type in the first box a number to start counting from.<br>
2.&nbspHave the user type in the second box a number where to stop counting to.<br>
3.&nbspHave the user type in a number to step up or down by when counting.<br>
<li type=disc>When the user presses the button this page will clear and the list of numbers will be displayed<br>
<li type=disc>If the user uses the same starting and ending number alert them that is not valid.<br>
<li type=disc>The user can enter a starting number bigger than the ending number.<br>
<li type="disc">Take into account that a user might enter a positive or negative step value<br>

<p align=center>
starting number&nbsp<input type="text" name="text4">&nbsp
ending number<input type="text" name="text5">
<p align=center>step up by<input type="text" name="text6">&nbsp
<input type="button" name="display" value="Display Numbers" onClick="displaynumbers()">

</body>
</html>

Thanks for any assistance/guidance.

shlagish
03-02-2004, 10:30 PM
I can see a couple problems here.
if a = 5 and b=2 and c = -3

the loop wont start since couter isn't smaller than b (couter=a=5 and b=2)
The loop says
couter =5
while 5 is smaller than 2, do the instructions.
Since 5 isn't smaller, the loop wont start.



If a is smaller that b and c is negative, the loop will never reach an end.


as for the 5+5+5+5 problem, your right, they are strings and act like strings (a+b=ab)
To make them numbers again, use ParseInt()
[code]
var a="2"; //the quotation marks make 2 be a string
var b="3"; //Same here
alert(a+b); //This will alert: 23
var c=parseInt(a);
var d=parseInt(b);
alert(c+d); //This will alert: 5
[code]

I don't know much about the parseInt() function, maybe someone else can fill up the blanks :)

Lisawynn
03-03-2004, 03:48 PM
Thanks. The parseInt worked great. Now I just have to figure out the negative number thing!!! Have a great day!!

Garadon
03-03-2004, 04:17 PM
function displaynumbers(){
var a=parseInt(document.form4.text4.value,10);
var b=parseInt(document.form4.text5.value,10);
var c=parseInt(document.form4.text6.value,10);

if(a==b)
{
alert("Your number selections are invalid.");
}
var d=b-a;
b=a+Math.sqrt(d*d);
for(counter=a;counter<=b;counter=counter+c)
{
document.write(a+(counter-a)*(d/Math.sqrt(d*d))+"<br/>");
}
}

Lisawynn
03-04-2004, 06:02 PM
That works great. How would you get the alert to display without getting the document.write to execute and open a new window?

Garadon
03-04-2004, 07:01 PM
if(a==b)
{
alert("Your number selections are invalid.");return
}

Lisawynn
03-04-2004, 07:17 PM
Thank you!