...

View Full Version : Setting the bgColor from a random number. Help please!



Tengil
01-18-2012, 11:46 AM
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.
Ive come up with a little bit of 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!

DaveyErwin
01-18-2012, 12:12 PM
this sort of thing won't do at all...
if(-10<newtemp<-5)
it should be ...
if(-10<newtemp && newtemp<-5)

Tengil
01-18-2012, 12:22 PM
Okay, thanks!
So if I change that bit it should work?

Philip M
01-18-2012, 12:34 PM
Okay, thanks!
So if I change that bit it should work?

Why not try it?

DaveyErwin
01-18-2012, 12:36 PM
You have to change every
place you have the
pattern ...

a<b<c

to

a<b && b<c

Tengil
01-18-2012, 12:40 PM
Why not try it?

Yeah sorry, I should have before I typed, but I was in a hurry.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum