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?

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

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Expressions_and_Operators#Comparison_operators

I have corrected the two instances where you need the “==”:

if (a1==0)){

and

}while (a1 ==0);

I still can not get it to return a value after the do loop and the if statements.

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.


var a1 = 1;

should be, instead:


a1 = 1;

Thanx for fixing the scoping problem but that did not fix the whole program:

a1 = 1;

Is my syntax correct for a string?

SunRise1 = hrr + “:” + mnr;

and

if (mn < 10){mn = “0” + mn}

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.