for loop maximum iterations

my code is not limiting the amount of loops to the maximum iterations

Page 1 of 1

8 Replies - 6412 Views - Last Post: 13 January 2011 - 02:02 PM

#1 havahale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 11

for loop maximum iterations

Posted 12 January 2011 - 12:02 PM

Can you help explain why my code is not limiting the loops to the maximum number of iterations I have defined?

<script type="text/javascript"> 
<!--javascript starts here
//prompt variables
var message = prompt("Please enter your name to proceed.", "");
var x = prompt("Please enter a number to proceed.");
//maximum number of iterations
var n = 10;
for (var n = 0; n < x; n++)
{
document.write(message + "<br />");
//writes the message string x number of times provided it is less than or equal to n
}
//-->
</script>

This post has been edited by Dormilich: 12 January 2011 - 12:21 PM
Reason for edit:: please use [code] /* your code source here */ [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: for loop maximum iterations

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: for loop maximum iterations

Posted 12 January 2011 - 12:24 PM

not sure what the script does at yours, it works fine for me (FF 3.6)
Was This Post Helpful? 0
  • +
  • -

#3 havahale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 11

Re: for loop maximum iterations

Posted 12 January 2011 - 12:34 PM

View PostDormilich, on 12 January 2011 - 11:24 AM, said:

not sure what the script does at yours, it works fine for me (FF 3.6)


Yes, it works for me too, however, if you put in 100000000000000000000000000000 there is nothing preventing the code from looping that many times which would really slow down your browser while it processes the script.

I would like to prevent the maximum number of iterations and limit it to a specific number using a variable defined as n
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: for loop maximum iterations

Posted 12 January 2011 - 12:48 PM

Wrap an ifcheck around that for loop to ensure that n is less than a certain amount that you decide is too many iterations.

This post has been edited by Kilorn: 12 January 2011 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1184
  • View blog
  • Posts: 7,254
  • Joined: 07-September 06

Re: for loop maximum iterations

Posted 12 January 2011 - 07:49 PM

Or before the for loop check if x is greater than what you deem maximum iterations and then (if you enter the if statement's body) set x to the maximum.

Ex:
if(x > 100){
  x = 100;
}

Was This Post Helpful? 1
  • +
  • -

#6 D.Mulroy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: for loop maximum iterations

Posted 13 January 2011 - 06:44 AM

First of all, from what I can tell you want your maximum iterations set to ten since that is what you originalyl declare n as BUT you then set n == 0 when you create the for loop
var n = 10;  
for (var n = 0; n < x; n++)  


instead try doing this

var n = 10
for (var i = 0; i < x; i++)
{
   if (i == n)
   {
      break;
   }
   document.write(message + "<br />"); 
}


Was This Post Helpful? 1
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: for loop maximum iterations

Posted 13 January 2011 - 08:11 AM

BetaWarís approach is to prefer, because you set the possible maximum outside the loop (and the condition does not need to be evaluated each time).
Was This Post Helpful? 0
  • +
  • -

#8 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: for loop maximum iterations

Posted 13 January 2011 - 01:37 PM

What you could also do is check to make sure that it is less then your maximum, while still being less then x:
for(var i=0; i < n && i < x; i++)


Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: for loop maximum iterations

Posted 13 January 2011 - 02:02 PM

thatís pretty much the same as testing the condition inside the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1