www.webdeveloper.com
Results 1 to 3 of 3

Thread: why do all browser lag for this given loop?

  1. #1
    Join Date
    Apr 2014
    Posts
    5

    Angry why do all browser lag for this given loop?

    var a,b;
    while (a!==b){
    if (a>b) {
    var a=a-b;
    }
    else {
    var b=b-a;
    }
    }

    so you enter numbers for a and b, and while the condition is true it is doing the given calculations and finally the a and b will became the same and the condition for WHILE will be false and after that it gives out a number a or b (doesnt matter cus both are equal already), also it works for some numbers but browser lags for some specific numbers for example if a=60 and b=120, i dont understand why by the way the given function calculates the largest common divisor of two given numbers, please help me and tell why is it lagging

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    tested your version in ff, works fine. a slightly more improved version
    Code:
    var a = 60,
    	b = 120;
    
    while(a !== b){
    	if(a > b){
    		a -= b; // same as "a = a - b"
    	}else{
    		b -= a;
    	}
    }
    //alert(a +' '+ b);

  3. #3
    Join Date
    Apr 2014
    Posts
    5
    oh thanks for the improvement I really like it much and for info too that it works for ff but in chrome it still lags i dont use ff

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles