Code that display a greeting message

Hi all,

i have a scipt that i want that to display a greeting message. i did some code but i am not sure whether this is right, because i can't display that out. Can anyone help me??
Thank in advance.

code
=======================
//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>")

//calling the greetings function
var holidayMsg = greetings()

//count the number of days from current date starting from jan
var currDay = new Date()
var currentDay = currDay.getDate()
var currentMonth = currDay.getMonth()

if(currentMonth == 1){
noOfDays = currentDay
}
if(currentMonth == 2){
noOfDays = currentDay + 31
}
if(currentMonth == 3){
noOfDays = currentDay +31 +28
}
if(currentMonth == 4){
noOfDays = currentDay +31 +28 +31
}
if(currentMonth == 5){
noOfDays = currentDay +31 +28 +31 +30
}
if(currentMonth == 6){
noOfDays = currentDay +31 +28 +31 +30 +31
}
if(currentMonth == 7){
noOfDays = currentDay +31 +28 +31 +30 +31 +30
}
if(currentMonth == 8){
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31
}
if(currentMonth == 9){
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31
}
if(currentMonth == 10){
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30
}
if(currentMonth == 11){
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30 +31
}
if(currentMonth == 12){
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30 +31 +30
}

//function of a switch
function greetings(){
switch(month){
case 0:

if(noOfDays >= 2 && noOfDays <= 43){
daysLeft = 43 - noOfDays
document.write("Only "+daysLeft+" days untill Valentine's day!")
}
break

case 1:
if(noOfDays == 45){
document.write("Happy Valentine's Day")
}
break

case 2:
//from 15th feb to 31st aug
if(noOfDays >= 46 && noOfDays <= 243){
document.write("Please check out our current range of gifts.")
}
break

case 3:
//from 1st sept to 24th dec
if(noOfDays >= 244 && noOfDays <= 287){
daysLeft = 287 - noOfDays
document.write("Only "+daysLeft+" days left untill Christmas. ")
}
break

case 4:
// from 24th dec to 1st jan
if(noOfDays >= 358 && noOfDays <= 366){
document.write("Merry Christmas and Happy New Year ")
}
break

default:
document.write("No current event coming")
}
}

=================
code end

diana
[3643 byte] By [dianazheng] at [2007-11-19 0:53:44]
# 1 Re: Code that display a greeting message
i see certain explainations as to why the greeting message is not displayed.
1.

//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>")

//calling the greetings function
var holidayMsg = greetings()

u display the message first and make a call(and in turn assign the greeting message) later...so at this point holidayMsg is "undefined".

2. u dont seem to be returning any thing from the function greetings... but u do a document.write...so even if i assume that point no. 1 is taken care of... the message wud not be printed in the manner u want.

if this does not solve the prob... feel free to get back.
PallaviDalvi at 2007-11-8 0:19:53 >
# 2 Re: Code that display a greeting message
Hi PallaviDalvi,

You are right, i am just careless, i forget that a function should be at the top.
And you are right too, even if i call the function at the bottom, i still can't display the output that i wanted.
Would you please help me to correct my code so that i can display the output.
dianazheng at 2007-11-8 0:20:53 >
# 3 Re: Code that display a greeting message
Place

//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>")

after the call to the function.
in the function change all document.write's to return().
hopefully that shud solve the prob.
PallaviDalvi at 2007-11-8 0:21:59 >
# 4 Re: Code that display a greeting message
sorry, i don't really get what you mean.

do you have msn or something else that i can ask you directly...
dianazheng at 2007-11-8 0:23:02 >
# 5 Re: Code that display a greeting message
in the function greetings()

document.write("Only "+daysLeft+" days untill Valentine's day!")

shud be replaced with

greeting_text="Only "+daysLeft+" days untill Valentine's day!";
return(greeting_text);

and

//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>")

//calling the greetings function
var holidayMsg = greetings()

shud be changed to

//calling the greetings function
var holidayMsg = greetings()

//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>")
PallaviDalvi at 2007-11-8 0:24:01 >
# 6 Re: Code that display a greeting message
i did what you stated and it did display something, but where does it display "Undfined " word again.
And i have a request, can you check my calculation, because it display the worng stuff.

thanks
dianazheng at 2007-11-8 0:24:57 >
# 7 Re: Code that display a greeting message
once u've made the corrections "undefined" shud not be displayed any where.
as for checking the calc... i need some time for that...
PallaviDalvi at 2007-11-8 0:26:01 >
# 8 Re: Code that display a greeting message
Thanks.

i am not sure why, but the "undefined" was display out in the website.
i have some other question about my program, can i ask you as well... because i just couldn't find anything i can use on the website.

do you have a messagner or somthing, so that i don't need to keep putting them on the forum

i have a msn messanger dianazheng@hotmail.com
diana
dianazheng at 2007-11-8 0:27:04 >
# 9 Re: Code that display a greeting message
well heres the code that doesnt show any undefined. however i didnt have the patience to go thru the logic.

//count the number of days from current date starting from jan
var currDay = new Date()
var currentDay = currDay.getDate()
var currentMonth = currDay.getMonth()

if(currentMonth == 1)
{
noOfDays = currentDay
}
if(currentMonth == 2)
{
noOfDays = currentDay + 31
}
if(currentMonth == 3)
{
noOfDays = currentDay +31 +28
}
if(currentMonth == 4)
{
noOfDays = currentDay +31 +28 +31
}
if(currentMonth == 5)
{
noOfDays = currentDay +31 +28 +31 +30
}
if(currentMonth == 6)
{
noOfDays = currentDay +31 +28 +31 +30 +31
}
if(currentMonth == 7)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30
}
if(currentMonth == 8)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31
}
if(currentMonth == 9)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31
}
if(currentMonth == 10)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30
}
if(currentMonth == 11)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30 +31
}
if(currentMonth == 12)
{
noOfDays = currentDay +31 +28 +31 +30 +31 +30 +31 +31 +30 +31 +30
}

//calling the greetings function
var holidayMsg = greetings(currentMonth,noOfDays);

//display the greeting message
document.write("<h3><center>"+holidayMsg+" <br> </center></h3>");

//function of a switch
function greetings(month,noOfDays)
{
switch(month)
{
case 0:

if(noOfDays >= 2 && noOfDays <= 43)
{
daysLeft = 43 - noOfDays
return("Only "+daysLeft+" days untill Valentine's day!")
}
break

case 1:
if(noOfDays == 45)
{
return("Happy Valentine's Day")
}
break

case 2:
//from 15th feb to 31st aug
if(noOfDays >= 46 && noOfDays <= 243)
{
return("Please check out our current range of gifts.")
}
break

case 3:
//from 1st sept to 24th dec
if(noOfDays >= 244 && noOfDays <= 287)
{
daysLeft = 287 - noOfDays
return("Only "+daysLeft+" days left untill Christmas. ")
}
break

case 4:
// from 24th dec to 1st jan
if(noOfDays >= 358 && noOfDays <= 366)
{
return("Merry Christmas and Happy New Year ")
}
break

default:
return("No current event coming")
}
}

as for the messenger ... its disabled here and so u wud have to post the queries on the forum only :(
PallaviDalvi at 2007-11-8 0:28:10 >
# 10 Re: Code that display a greeting message
i have try out your code, and it works without the "undefined" words being display.
I try change mine to yours, actually the same but it still display the "undefined" words and "no current event coming".

help help...
dianazheng at 2007-11-8 0:29:08 >
# 11 Re: Code that display a greeting message
well thats funny! coz i did not re-write any code. the whole thing is just a re-structuring of ur code. i have just changed the order of ur statements! match the order of statements in both the codes ... that shud do the trick. :thumb:
PallaviDalvi at 2007-11-8 0:30:07 >
# 12 Re: Code that display a greeting message
I don't know why it that so.

Do you have ideas to help me...
dianazheng at 2007-11-8 0:31:12 >
# 13 Re: Code that display a greeting message
none other than the one i've already given... check the order of statements.
PallaviDalvi at 2007-11-8 0:32:08 >
# 14 Re: Code that display a greeting message
I tried changing the order of my code as well... but that didn't helps too.

Do you think, there is something wrong with my code...
dianazheng at 2007-11-8 0:33:13 >
# 15 Re: Code that display a greeting message
Just want to make sure that i change the corect thing:
These is what you have change to:

code
============
//function of a switch
function greetings(month, noOfDays){

//calling the greetings function
var holidayMsg = greetings(currentMonth, noOfDays)

return("something")

=============
code end
dianazheng at 2007-11-8 0:34:07 >
# 16 Re: Code that display a greeting message
ya...thats pretty much what i changed. why dont u try copying the modified code(one i submitted) over the previous code... maybe that helps. or else theres absolutely nothign i did new...
PallaviDalvi at 2007-11-8 0:35:16 >
# 17 Re: Code that display a greeting message
Now slove the problem with the "undefined".

Then the problem now is that it does not display the correct greeting message according to the system dates...

help help....
dianazheng at 2007-11-8 0:36:13 >