Current location: Hot Scripts Forums » General Web Coding » JavaScript » Infinite while loop


Infinite while loop

Reply
  #1 (permalink)  
Old 11-01-11, 06:52 PM
jw2 jw2 is offline
New Member
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Infinite while loop

Hi everyone,

I'm a beginner and have been experimenting with loops in javascript. Could anybody point me in the right direction as to why my code returns a script error/infinite loop?

Code:
<html>
<script>

// Date:        November 2011
// Purpose:     Get an odd number between 25 and 75

var num=0;

while ((num<25) || (num>75) && (num%2 ==0));
{
	num = eval(prompt("Enter an odd number between 25 and 75"));
}

alert(num + " is an odd number between 25 and 75");

           
</script>
</html>
Thanks!
Reply With Quote
  #2 (permalink)  
Old 11-01-11, 11:46 PM
KatanaPL KatanaPL is offline
New Member
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
I don't know JS but I'm thinking that you are asking the user to type a number when num is <25 OR >75 and the mod2 is 0
BUT num is 0 at the start, so it will always ask for a number.
You should ask the user for a number THEN check if the number is <25, >75 and mod2 is 0.

Alert should be inside the loop, not the prompt

Code:
<html>
<script>

// Date:        November 2011
// Purpose:     Get an odd number between 25 and 75

var num=0;

num = eval(prompt("Enter an odd number between 25 and 75"));

while ( ((num>=25) || (num<=75)) && (num%2 ==1))
{
    alert(num + " is an odd number between 25 and 75");
    break;
}



           
</script>
</html>
It's working now

Last edited by KatanaPL; 11-01-11 at 11:54 PM.
Reply With Quote
Reply

Bookmarks

Tags
infinite, while loop script error


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
assembly language using notepad++, TASM, and TLINK caierhui Other Languages 0 03-18-10 10:50 AM
infinite loop versus guarded blocks UnrealEd Everything Java 0 11-11-07 02:53 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
While loop jaishalg PHP 1 11-23-04 03:36 PM
SQL infinite loop problem with VB application Syed Visual Basic 2 05-07-04 10:19 AM


All times are GMT -5. The time now is 02:05 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.