Jump to content

Javascript - Good Day Script

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Martijn

Martijn

    Newbie

  • Members
  • Pip
  • 2 posts
Hello, thanks for viewing. I'm trying to learn javascript and I was reading some tutorials.

right here

They showed an example of an script that shows "Good Morning, Or good day etc..." I didn't find it was quite perfect yet so I tried to edit it an little and ended up with this.

<script type="text/javascript">

var d = new Date();

var time = d.getHours();

if (time<10) 

{

document.write("Good morning!");

}

else if(time>10 && time<18)

{

document.write("Good Afternoon!");

}

else (time>18 && time<10)

{

document.write("Good Evening!");

</script>

It doesn't seem to work, and I don't know what the bug is. Can anyone help me?
I'm thinking I did something wrong with the "Time > 18 && time <10"
But I'm not sure.

Duck tape is like the force. It has a light side, a dark side, and it holds the world together.


#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Well first you forgot your closing brace in your last else clause. You never account for the fact that time can equal 10 or 18, and lastly time can never be greater than 18 and less than 10 at the same time, so your last else clause is useless. I'm no javascript expert, but give this a go:

if (time<=10) 
{
document.write("Good morning!");
}
else if(time>10 && time<18)
{
document.write("Good Afternoon!");
}
else (time>=18)
{
document.write("Good Evening!");
}

That should take care of the logic.