Closed Thread Icon

Topic awaiting preservation: Loop Problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8806" 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 08-11-2003 01:20

Hi, guys. How are you? Did you have good week end?
It was pretty boring to me, unfortunately. But a friend of mine made wonderful Japanese meal for me. It was great!

By the way, can I ask you this problem, please?
I am coding script using loop. My exercise is following:

code:
<html>
<head>
<script type="text/javascript">
function cal(){

var passw="san";
var guess=form1.pass.value;
var i=0;

while(guess != passw){

i=i+1;
alert("Try again");
return false;

}

form1.ans.value="you";
return i;
form1.time.value=i;

}
</script>
</head>
<body>
<h3>Try to get Info about me!</h3>
<form name="form1">
<P>Enter the password: <input type="password" name="pass" /></p>
<p><input type="button" value="Submit" onClick="cal()"/>
<input type="reset" value="Reset" /></p>
<p>I love....<input type="text" name="ans" />!</p>
<p>It took <input type="text" name="time" size="3"/> times!</p>
</form>
</body>
</html>



My problem is:

1. User cannot get infor even though it is right password.

Hmm.....Would you see something wrong??? Please help me!!!

Hiroki Kozai

[This message has been edited by Hiroki (edited 08-11-2003).]

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-11-2003 01:32

Hi, guys.
Sorry to bother you. I found an error.
It was pretty typo-error.
But still one problem.....
I don't see why I cannot get the number of attempation.
I could get information of first text box.
thanks.

Hiroki Kozai

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 08-11-2003 11:14

Hiroki: You declared you counter i locally and reset it to 0 every time the cal() function is called. The user enters the good password or not and then you do what you have to. Nothing more, nothing less. So there's no need for a while(...) loop. You realise that the return instruction stops the exectution of a function, thus your counter will always be at 0 and the form1.time.value = i; statement will never be executed.

So here we go:

code:
var i=0;
function cal()
{
var passw = "san";
var guess = form1.pass.value;
if( guess != passw )
{
i++;
alert( "Try again" );
return false;
}
form1.ans.value = "you";
form1.time.value = i;
return i;
}

I beg your pardon if my questions offend you, but the way you code makes me wonder if you understand what you actually write and if you write all your functions in row and then test the whole thing praying that everything will work.

Let me again advice you to always have the documentation open and to use it. Code step by step, Don't hesitate to make dozens of preliminary tests. In a first time try to solve your problems by yourself then ask other people. Forcing yourself to find some solutions will increase your skills and you'll remeber the solutions which is not sure if you ask some helps as soon as your forget a semicolon.

Hope that helps.

Mathieu "POÏ" HENRI

[This message has been edited by poi (edited 08-11-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-11-2003 17:10

It also worries me that, as your code becomes more complex, the way in which you format it seems to get messier, and I wonder if that's linked to a possible lack of intuition about the way the language works. Perhaps, if you learn to indent your code better, it might actually improve your understanding of what you write.

Every time you open a block (a block is anything which uses { ... } ), you should indent by an extra tab, and when you close the block, you should unindent. For example:

code:
if (blahblahblah)
{
// indented by one tab here
}



If you have an if/else/else if structure, you should keep all of the if's at the same indentation level, like this:

code:
if (blahblahblah)
{
// indented here
}
else if (blahblahblah)
{
// indented here
}
else (blahblahblah)
{
// indented here
}



Also note how Poi's code is indented. The indentation goes in once when a block starts, and comes back out when the block ends.

Whether or not you choose to write your brackets like this:

code:
function blah()
{
...
}



or like this:

code:
function blah() {
...
}



or even like this:

code:
function blah() {
...
}
// unindented again here



is up to you, but try to maintain a consistent style.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-12-2003 01:30

Hi, Poi & Slime.
Thank you for telling me that.
Pretty glad to hear from you.
Now my code is working.
And thanks for those advices.
Yes, I will bear them in mind.
Thanks a lot, again.
cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu