Click to See Complete Forum and Search --> : Code that display a greeting message


dianazheng
September 30th, 2004, 12:16 AM
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

PallaviDalvi
September 30th, 2004, 02:51 AM
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.

dianazheng
September 30th, 2004, 04:05 AM
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.

PallaviDalvi
September 30th, 2004, 04:40 AM
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.

dianazheng
September 30th, 2004, 04:45 AM
sorry, i don't really get what you mean.

do you have msn or something else that i can ask you directly....

PallaviDalvi
September 30th, 2004, 04:50 AM
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>")

dianazheng
September 30th, 2004, 05:05 AM
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

PallaviDalvi
September 30th, 2004, 05:18 AM
once u've made the corrections "undefined" shud not be displayed any where.
as for checking the calc... i need some time for that...

dianazheng
September 30th, 2004, 05:38 AM
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

PallaviDalvi
October 1st, 2004, 12:33 AM
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 :(

dianazheng
October 1st, 2004, 01:23 AM
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...

PallaviDalvi
October 1st, 2004, 01:41 AM
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:

dianazheng
October 1st, 2004, 01:52 AM
I don't know why it that so.

Do you have ideas to help me....

PallaviDalvi
October 1st, 2004, 01:58 AM
none other than the one i've already given... check the order of statements.

dianazheng
October 1st, 2004, 02:06 AM
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
October 1st, 2004, 02:19 AM
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

PallaviDalvi
October 1st, 2004, 02:36 AM
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....

dianazheng
October 1st, 2004, 03:43 AM
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.....