Click to See Complete Forum and Search --> : Multiple if() conditions
Amaroqwlf
10-03-2005, 01:37 AM
How do I set multiple conditions that I want met?
This:
if(condition1)
{
if(condition2)
{
action1
}
}
hasn't worked at all for me. Is there any way I can set multiple conditions, such as:
if(number > 22)
{
if(number < 30)
{
document.write("That number is between 22 and 30")
}
}
I'm trying to create a javascript that will say "Good Evening," "Good Morning," etc. based on the time of day. Hope that wasn't too confusing. :rolleyes:
herodote92
10-03-2005, 02:08 AM
<script type="text/javascript">
var my_hour = new Date().getHours();
var msg = "Good night";
if (my_hour > 4) {
if (my_hour < 12) {
msg = "Good morning";
}
else {
if (my_hour < 18) {
msg = "Good afternoon";
}
else {
if (my_hour < 22) {
msg = "Good evening";
}
}
}
}
alert(msg);
</script>
baconbutty
10-03-2005, 02:44 AM
if(number > 22 && number < 30)
{
document.write("That number is between 22 and 30")
}
Amaroqwlf
10-03-2005, 11:53 AM
Thank you very much, both of you. :)
But why is it that your script works with the if() inside another if(), herodote92?
Whenever I try something like that, it just ignores the whole thing... :confused:
Anyway, this is my new, working script. I decided I liked baconbutty's && method. :D
<html>
<head>
<script language="javascript">
function date()
{
now = new Date()
hour = now.getHours()
}
function greeting()
{
date()
if(hour > 2 && hour < 12)
{
timeword = "Good Morning!"
}
if(hour > 11 && hour < 18)
{
timeword = "Good Afternoon!"
}
if(hour > 17 && hour < 23)
{
timeword = "Good Evening!"
}
if(hour > 22)
{
timeword = "You're on rather late..."
}
if(hour < 3)
{
timeword = "It's already past midnight!"
}
}
</script>
</head>
<body>
Hello everyone, and welcome to my website!
<script language="javascript">
greeting()
document.write(timeword)
</script>
</body>
</html>
Hope you don't mind that it takes up a lot of space. :p
herodote92
10-03-2005, 12:09 PM
why is it that your script works with the if() inside another if(), herodote92?
Perhaps because I was very careful with opening and closing brackets, in which I was considerably helped by judicious indentations (indentations are a marvellous invention :D )
The way you suggested will also work, only you do unneccessary tests. If the 1st test succeeds, than you don't need the following ones, etc. This is not a big problem if this part of the script is executed only once, but if it's inside a loop, it might be time-consuming. Also it's a little, ehm, heavy way of programming.
baconbutty
10-04-2005, 06:26 AM
You could also consider the "switch" statement:-
var timeword;
function greeting()
{
var now=new Date();
var hour=now.getHours();
timeword="";
switch(true):
{
case (hour>=0 && hour <12):
timeword += "Good Morning!";
case (hour <=3):
timeword += "It's already past midnight!"
break;
case (hour > 11 && hour < 18):
timeword += "Good Afternoon!"
break;
case (hour>= 18 && hour <=23):
timeword += "Good Evening!";
case (hour >= 22)
timeword += "You're on rather late..."
break;
default:
break;
}
}
herodote92
10-04-2005, 09:22 AM
<html>
<head>
<title>Good whatever...</title>
<script type="text/javascript">
var msgs = new Array();
msgs[0] = "Good morning !";
msgs[1] = "Good afternoon !";
msgs[2] = "Good evening !";
msgs[3] = "You're on rather late...";
msgs[4] = "It's already past Midnight...";
var m = "444000000000111111222223";
</script>
</head>
<body>
<script type="text/javascript">
var now = new Date();
alert(msgs[parseInt(m.substr(now.getHours(),1))]+"\n"+"In case you're computer isn't on time, please disconsider this message");
</script>
</body>
</html>
:D