Closed Thread Icon

Topic awaiting preservation: loop problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8694" title="Pages that link to Topic awaiting preservation: loop problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: loop problem <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-05-2003 23:37

Hi, guys. How are you today?
Would you have a time to read this??
My mission is:

quote:
Write a program that outputs(down the page) 10, 9, 8....1, Blastoff.
User input start number and endnumber. In above example, 10 is start number
and 1 is end number.



Here is my code:

code:
<html>
<head>
<script type="text/javascript">
document.write("Enter the number you wanna start");
function cal(){

for(i=document.theForm.num.value; i=document.theForm.end.value; i--)
{
document.write(i + " ");
}
document.write("Blast Off");
}
</script>
</head>
<body>


<form name="theForm">
<p>Enter the start number.
<input type="text" name="num" >
</p>
<p>Enter the end number.
<input type="text" name="end">
</p>
<input type="button" value="Get number" onClick="cal()">
<input type="reset" value="Reset">
</form>
</body>
</html>



When I ran the code, I only got start number. for example, if I input 100 for start num and 20 for end num, output is only 100. Not like 100, 99, 98, 97.....20, blastoff.
I am very suspisious about my loop. Would you see any problem???

for(i=document.theForm.num.value; i=document.theForm.end.value; i--)

Also error message said that document.theForm.end is not object. What does it mean??? Please help me!

Hiroki Kozai

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted posted 06-06-2003 11:18

Just a guess, but when you rewrite the document on the first time through the loop, the browser may be forgetting that it has something else to do. You might try writing the output to a textarea input.


Few problems in life can't be solved by chocolate

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 06-06-2003 14:33

right now your second page has nothing on it but "i(number) Blastoff" because thet's all you printed to it... you gotta imagine it's a completely different text file....

In other words, when you page only has "(number) Blastoff" it doesn't have any scripting on it.... does that make sense?

Why not start by rewriting the number in an input field all the way to "Blastoff" and *then* working on this?

Right now you seem to be working on two things you're unfamiliar with instead of just one.... some famous general said something along the lines of "It is extremely unwise to fight a war on two fronts"... yeah, I think this what I'm talking about..


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 06-06-2003 23:22

The problem with this line, for(i=document.theForm.num.value; i=document.theForm.end.value; i--), is that it is causes the loop to only run once.

It should be: for(i=document.theForm.num.value; i>=document.theForm.end.value; i--)

This will cause the i variable to start with a value of document.theForm.num.value and run throug the loop as long as i is greater or equal to document.theForm.end.value and for each time it runs through the loop it will decrease the value of i with one.

_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"

[This message has been edited by Veneficuz (edited 06-06-2003).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-07-2003 01:56

Actually that's only going to run once too since he's decrementing i, in this case your basically doing (i =3; i >= 3; i--)



.:[ Never resist a perfect moment ]:.

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 06-07-2003 12:32

Not really.. As long as the document.theForm.num.value is greater than document.theForm.end.value it will work. The code would then resemble something like for(i=5; i>=3; i--), which should work.

_________________________
"There are 10 kinds of people; those who know binary, those who don't and those who start counting at zero"

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-10-2003 23:54

Hi, guys. How are you today?
Well, it is bloody cold here, NZ.
Very difficult to cope with this weather. I miss warmer weather.
Never mind.
Many many thanks for your replies, guys.
Sorry to be late to reply.
I have been trying to figure out what the problem is.
But I am afraid I haven't hit it yet.
Here is my code:

code:
<html>
<head>
<script type="text/javascript">

var i=eval(document.theForm.num.value);
var k=eval(document.theForm.end.value);

function cal(){

for(i=document.theForm.num.value; i>=k; i--)
{
document.write(i + " ");
}
document.write("Blast Off");
}
</script>
</head>
<body>


<form name="theForm">
<p>Enter the start number.
<input type="text" name="num" >
</p>
<p>Enter the end number.
<input type="text" name="end">
</p>
<input type="button" value="Get number" onClick="cal()">
<input type="reset" value="Reset">
</form>
</body>
</html>



What I am doing is simple loop exercise. But it seems to be difficult enough for me.
Just error and error and error.
Here is my uploaded file.

I guess my counter is wrong. But I did like you guys told me.
Hmm.....Why???

Hiroki Kozai

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 06-11-2003 00:16

Try this:

code:
<html><head>

<script type="text/javascript">

var txt = ""

function blastoff(n1,n2){
for(i=n1; i>=n2; i--) {txt += i + "...<br>";}
txt += "Blast Off!!!";
document.write(txt);
}

</script>

</head><body>

<form name="theForm">
<p>Enter the start number: <input type="text" name="n1" ></p>
<p>Enter the end number: <input type="text" name="n2"></p>
<input type="button" value="Get number" onClick="blastoff(this.parentElement.n1.value,this.parentElement.n2.value)">
<input type="reset" value="Reset">
</form>

</body></html>



I always found it much easier to pass form elements to javascript straight into the form function call to the function itself. It's also always a good practice not to re-assign a variable to use as a loop counter. If you keep the counter variable seperate from the rest of the function it just makes it easier to work with.



[This message has been edited by Dracusis (edited 06-11-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu