# Getting out of a loop

• May 8, 2012, 17:07
Philosophaie
Getting out of a loop
SolarZenith functions correctly. I have tried two different loops and neither produce the right answer.

I have tried placing the "return Sunrise1" right after "Sunrise1 = hrr + "." + mnr;" and it produces "undefined" in the textbox.

How do I get a loop to be able to save an variable till the loop is concluded?

Code:

```function SunRise(mo, dy, yr, hr, mn, timezone, dst, lon, lat){     var Twi = 90.8333333333333;var t = 0;     do{             hr = Math.floor(t / 60);             mn = t - 60 * hr;         solarzen = SolarZenith(mo, dy, yr, hr, mn, timezone, dst, lon, lat);         if (solarzen < Twi){// Twilight             //Light             if (a1 = 0){               var a1 = 1;               var hrr = hr;               if (mn < 10){mn = "0" + mn}               var mnr = mn;                                 SunRise1 = hrr + ":" + mnr;                         }//end if                 }//end if     t++;         }while (a1 = 0);     return SunRise1; }//End Function```
• May 8, 2012, 18:34
Logic Ali
• May 9, 2012, 10:16
Philosophaie
I have corrected the two instances where you need the "==":

Quote:

if (a1==0)){
and

Quote:

}while (a1 ==0);
I still can not get it to return a value after the do loop and the if statements.
• May 9, 2012, 10:49
ParkinT
It appears you are suffering from a scoping problem.
Because you [re]assign 'a1' with the var keyword, it creates another variable inside the scope of the 'do'
So the 'a1' you are testing in the 'while' is NOT changed.
Code:

`var a1 = 1;`
Code:

`a1 = 1;`
• May 9, 2012, 13:02
Philosophaie
Thanx for fixing the scoping problem but that did not fix the whole program:

Quote:

a1 = 1;
Is my syntax correct for a string?

Quote:

SunRise1 = hrr + ":" + mnr;
and

Quote:

if (mn < 10){mn = "0" + mn}
• May 9, 2012, 20:52
Logic Ali
Quote:

Originally Posted by Philosophaie
Thanx for fixing the scoping problem but that did not fix the whole program:

It seems you're trying to get the result into a textbox. I think you need to show your current code in context, to clarify how it is being used.