Closed Thread Icon

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

 
The Jackal
Paranoid (IV) Inmate

From: The Dark Side of the Moon
Insane since: Jun 2000

posted posted 09-06-2003 20:36

OK, here is the problem. I want to limit the input in the prompt to be 0-25. If the user inputs a number higher than this or uses letters, I want to have the input box pop back up and tell them to enter a valid number. Here is the code I have:

code:
var myNumber = parseFloat(prompt("Enter a number between 1 and 25",""));
var loopCounter;
if (!(myNumber <= 25)) {
while (!(myNumber <= 25)) {
parseFloat(prompt("This is an invalid entry. Enter a number between 1 and 25","")); }
}
else {
for (loopCounter=0; loopCounter != myNumber; loopCounter++) {
document.write("My nifty code here " + loopCounter + ".<P>"); }
}



The problem is that once a person makes an invalid entry, I get an infinite while loop. Any ides on what I am doing wrong or what I can do different? Thanks.



[This message has been edited by The Jackal (edited 09-06-2003).]

[This message has been edited by The Jackal (edited 09-06-2003).]

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 09-06-2003 21:21

The reason you get the infinite while loop is that you never change myNumber, you should add 'myNumber= ' in front of the second promt message.

Played some with the code and came up with this working example:

code:
var myNumber = parseFloat(prompt("Enter a number between 1 and 25",""));
var loopCounter;
if (!(myNumber <= 25) && myNumber > 1)
{
while (!(myNumber <= 25))
{
myNumber = parseFloat(prompt("This is an invalid entry. Enter a number between 1 and 25",""));
}
}

document.write("<ul>");
for (loopCounter=0; loopCounter < myNumber; loopCounter++)
{
document.write("<li>My nifty code here " + loopCounter + ".</li>");
}
document.write("</ul>");



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

Petskull
Maniac (V) Mad Scientist

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

posted posted 09-07-2003 05:18

damn! Beat me to it! *ahem* ...by a lot...


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

« BackwardsOnwards »

Show Forum Drop Down Menu