Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting the bgColor from a random number. Help please!

    Hi!
    I new my webpage to change the bgColor dependent on the outside temperature, which will be randomised. Also, i want to check which month it is first, so I can get an appropriate temperature for the season.
    The first three ifs are to check which season it is and then set an appropriate outside temp. After them i have several ifs which i want to set the bgColor depending on the appropriate outside temperature.
    I´ve come up with a little bit of code:

    Code:
    	var temp = Math.floor(Math.random()*16);
    
    	var d = new Date();
    	var present = (d.getMonth()+1);
    
    	if ((present==12)||(present==1)||(present==2)){
    	newtemp=temp-20;
    	
    	}
    
    	if ((2<present<6)||(8<present<12)){
    	newtemp=temp-5;
    	
    	}
    	if (5<present<8){
    	newtemp=temp+10;
    	
    		}
    
    
    	if (newtemp<-9)
    	{
    	document.body.style.backgroundColor="#8FD8D8";
    	}
    	if(-10<newtemp<-5)
    	{
    	document.body.style.backgroundColor="#0276FD";
    	}
    	if(-6<newtemp<0)
    	{
    	document.body.style.backgroundColor="#B7C3D0";
    	}
    	if(1<newtemp<6)
    	{
    	document.body.style.backgroundColor="#458B00";
    	}
    	if(5<newtemp<11)
    	{
    	document.body.style.backgroundColor="##FFE600";
    	}
    	if(10<newtemp<16)
    	{
    	document.body.style.backgroundColor="#CD0000";
    	}
    	if(15<newtemp<26)
    	{
    	document.body.style.backgroundColor="#660000";
    	}
    Would greatly appreciate input!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    this sort of thing won't do at all...
    if(-10<newtemp<-5)
    it should be ...
    if(-10<newtemp && newtemp<-5)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, thanks!
    So if I change that bit it should work?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Tengil View Post
    Okay, thanks!
    So if I change that bit it should work?
    Why not try it?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    You have to change every
    place you have the
    pattern ...

    a<b<c

    to

    a<b && b<c

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Why not try it?
    Yeah sorry, I should have before I typed, but I was in a hurry.


  •  

    Posting Permissions

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