Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: What will break, and what will break it? [closed]

Potentially dangerous code below, line 2
Repeat: POTENTIALLY DANGEROUS CODE BELOW
var history = ""; 
for (start = 3; start = 1000; start += 1){
  if(start % 5 == 0 || start % 3 == 0) 
    history += start + " "; }

Okay, this is the tenth time I've put JavaScript code in that's frozen my browser. It's putting my computer in shock. Are these panic attacks going to destroy her heart? Where can I learn about all the crap that might break my computer as I continue to learn and practice JavaScript? I'm looking for an exhaustive list, only.

like image 488
Wolfpack'08 Avatar asked Mar 19 '26 19:03

Wolfpack'08


1 Answers

Your loop: for (start = 3; start = 1000; start += 1){
The second part of a for( ; ; ) loop is the condition test. The loop will continue until the second part evaluates to false. To not create an infinite loop, change your code to:

for (var start = 3; start < 1000; start += 1){

Note: start+=1 is equal to start++. If you want a compact code, you can replace +=1 by ++.

An overview of the three-part for-loop, for(initialise; condition; increment):

  • initialise - Create variables (allowed to be empty)
  • condition - The loop will stop once this expression evaluates to false
  • increment - This expression is executed at the end of the loop
    Always check against infinite loops: Make sure that the condition is able to evaluate to false.

Commonly made mistakes:

  • A negative incremental expression in conjunction with a is-lower-than comparison:
    i-- decreases the counter, so i<100 will always be true (unless the variable i is initialized at 100)
  • A positive incremental expression in conjunction with a is-higher-than comparison.
  • A non-incrementing expression: for(var i=0,j=0; i<100; j++) (i doesn't increase)
  • A condition which is always true (such as in your case)
like image 55
Rob W Avatar answered Mar 21 '26 07:03

Rob W