SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,335
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    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;
    should be, instead:
    Code:
    a1 = 1;
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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}

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Philosophaie View Post
    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.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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