SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    JS If statements are not working

    Hi guys,
    I apologise for any complexity of the following script, but what I am trying to achieve is
    1.) At 6:51 am, the image sunrise.png is written as the background of #wrap
    2.) At 12:04 midnight through 12:07 midnight, the background of #wrap is green.png
    then
    3.) Then, at a random time at night that is not 6:51 am nor in-between 12:04 midnight & 12:07 midnight, the background of #wrap is set as aurora.png

    This is the code of if statments that I wrote to achieve it:
    Code JavaScript:
    <script type="text/javascript">
    			var timeMinutes=new Date();
    			var timeHours=new Date();
    			var sunriseHour=6;
    			var sunriseMinutes=51;
    			var greenHour=0;
    			var greenMinutes=4;
    			var greenMinutesEnd=7;
    			var randomTime=Math.floor(Math.random()*11);
    			var randomTimeEnd=randomTime + 1;
     
    			if(randomTime=sunriseHour)
    				{
    					randomTime=Math.floor(Math.random()*11);
    				}
    			else if(randomTime=greenHour)
    				{
    					randomTime=Math.floor(Math.random()*11);
    				}
    			else {
    				if(timeHours.getHours()=sunriseHour && timeMinutes.getMinutes()=sunriseMinutes)
    					{
    						document.write('<style type="text/css">#wrap{background:transparent url(img/sunrise.png) center top no-repeat;}.headerText span{color:#3c0376; text-shadow:none;}</style>');
    					}
    				else if((timeHours.getHours()=greenHour && timeMinutes.getMinutes()=greenMinutes) || (timeHours.getHours()=greenHour && ((timeMinutes.getMinutes()<greenMinutesEnd) && (timeMinutes.getMinutes()>greenMinutes))))
    					{
    						document.write('<style type="text/css">#wrap{background:transparent url(img/green.png) center top no-repeat;}.headerText span{color:#3c0376; text-shadow:none;}</style>');
    					}
    				else if(timeHours.getHours()=randomTime || timeHours.getHours()<randomTimeEnd)
    					{
    						document.write('<style type="text/css">#wrap{background:transparent url(img/aurora.png) center top no-repeat;}</style>');
    					}
    				else{
    					document.write('<style type="text/css">#wrap{background:transparent url(img/space.png) center top no-repeat;}</style>');
    				}
    			}
    		</script>

    It is not producing any errors, but it is not wroking either.

    Would anyone be willing to help me fix it and see if I am trim it down any?

    I would really appreciate any help

    Thanks in Advance & I hope to hear from you soon,
    Team 1504

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,824
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    if(randomTime=sunriseHour)

    this assigns the value of sunriseHour to randomTime and then tests if randomTime is true.

    Did you perhaps mean:

    if(randomTime===sunriseHour)

    which compares randomTime to sunriseHour and if they are identically equal returns true
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you that worked perfectly.

    I thought about that, but stupid w3schools told me the single = was okay.

    I am seriously considering not using them as a resource anymore because they already damaged my knowledge of CSS a few years ago and now this....

    Thank you again & Kind Regards,
    Team 1504

    Does the random time at night that is not occupied by the other set times logic look good?


Tags for this Thread

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
  •