SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Please help TheN00B :)

    <script type="text/javascript">

    time = d.getHours()
    day = new Date()
    day_number = day.getDate()
    weekday = day.getDay()+1
    month = day.getMonth()+1
    year = day.getYear()


    if (time < 6)
    {
    document.write("Good night")
    }
    if (time < 12 && time > 6) {
    document.write("Good morning")
    }
    if (time < 18 && time > 12)
    {
    document.write("Good afternoon")
    }
    if (time < 24)
    {
    document.write("Good night")
    }
    <br>
    document.write("Now it is, ")
    document.write(time)
    if (weekday ==1)
    {
    document.write("Sunday,")
    }
    if (weekday ==2)
    {
    document.write("Monday,")
    }
    if (weekday ==3)
    {
    document.write("Tuesday,")
    }
    if (weekday ==4)
    {
    document.write("Wednesday,")
    }
    if (weekday ==5)
    {
    document.write("Thursday,")
    }
    if (weekday ==6)
    {
    document.write("Friday,")
    }
    if (weekday ==7)
    {
    document.write("Saturday,")
    }

    document.write(day_number," of ")

    if (month ==1)
    {
    document.write("january")
    }
    if (month ==2)
    {
    document.write("february")
    }
    if (month ==3)
    {
    document.write("march")
    }
    if (month ==4)
    {
    document.write("april")
    }
    if (month ==5)
    {
    document.write("may")
    }
    if (month ==6)
    {
    document.write("june")
    }
    if (month ==7)
    {
    document.write("july")
    }
    if (month ==8)
    {
    document.write("august")
    }
    if (month ==9)
    {
    document.write("september")
    }
    if (month ==10)
    {
    document.write("october")
    }
    if (month ==11)
    {
    document.write("november")
    }
    if (month ==12)
    {
    document.write("december")
    }

    document.write(" of ",ano)
    </script>

    Please help me, ty for your atention
    TheN00B
    i am really a n00b

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry but you need to be far more descriptive. Both in your title and in why do you need help? I can guess let me try.

    1 - you don't know what the code does
    2 - there is an error but you aren't sure where or why and would like help fixing it
    3 - An alien stole your code and left you with this

    Seriously you should be more descriptive. Also before posting the code you should surround it the code formatter tage
    Code:
    this would be your code
    Now to figure out what is up...

    Code:
    { 
    document.write("Good night") 
    } 
    <br> 
    document.write("Now it is, ")
    See the <br> tag it can't exist within javascript you need to write it. or remove it. so try this.

    Code:
    { 
    document.write("Good night") 
    } 
     
    document.write("<br />Now it is, ")
    Now for the second error. You have defined an unamed variable at the top and in the wrong spot so it can't even fetch the time from the date object.

    Code:
    time = d.getHours() 
    day = new Date() 
    day_number = day.getDate() 
    weekday = day.getDay()+1 
    month = day.getMonth()+1 
    year = day.getYear()
    should be

    Code:
    day = new Date() 
    time = day.getHours() 
    day_number = day.getDate() 
    weekday = day.getDay()+1 
    month = day.getMonth()+1 
    year = day.getYear()
    And for the last error which isn't fatal so the code still works.
    Code:
    document.write(" of ",ano)
    What is this. I couldn't find anything to reference i so that I could fix it so I simply removed it. But to tell you the error you are trying to fetch a variable called ano which doesn't exist in your code. and he line should look like this

    Code:
    document.write(" of "+ano)
    Hope that helps
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ty, but it still not working :/

    Thanks, but it is still not working :/

    i changed the code for :

    <script type="text/javascript">
    day = new Date()
    time = d.getHours()
    day_number = day.getDate()
    weekday = day.getDay()+1
    month = day.getMonth()+1
    year = day.getYear()


    if (time < 6)
    {
    document.write("Good night")
    }
    if (time < 12 && time > 6) {
    document.write("Good morning")
    }
    if (time < 18 && time > 12)
    {
    document.write("Good afternoon")
    }
    if (time < 24)
    {
    document.write("Good night")
    }

    document.write("<br />Now it is, ")
    document.write(time)
    if (weekday ==1)
    {
    document.write("Sunday,")
    }
    if (weekday ==2)
    {
    document.write("Monday,")
    }
    if (weekday ==3)
    {
    document.write("Tuesday,")
    }
    if (weekday ==4)
    {
    document.write("Wednesday,")
    }
    if (weekday ==5)
    {
    document.write("Thursday,")
    }
    if (weekday ==6)
    {
    document.write("Friday,")
    }
    if (weekday ==7)
    {
    document.write("Saturday,")
    }

    document.write(day_number," of ")

    if (month ==1)
    {
    document.write("january")
    }
    if (month ==2)
    {
    document.write("february")
    }
    if (month ==3)
    {
    document.write("march")
    }
    if (month ==4)
    {
    document.write("april")
    }
    if (month ==5)
    {
    document.write("may")
    }
    if (month ==6)
    {
    document.write("june")
    }
    if (month ==7)
    {
    document.write("july")
    }
    if (month ==8)
    {
    document.write("august")
    }
    if (month ==9)
    {
    document.write("september")
    }
    if (month ==10)
    {
    document.write("october")
    }
    if (month ==11)
    {
    document.write("november")
    }
    if (month ==12)
    {
    document.write("december")
    }

    document.write(" of "+ year)
    </script>

  4. #4
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking ops, sorry it is working :/

    Sorry it is working, i didnīt correct it for
    time = day.getHours()
    Sincerily sorry for bothering you
    Thank you very much

    TheN00B
    Really A N00B

  5. #5
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Ty, but it still not working :/

    No bother...I enjoy helping. Glad it worked
    Last edited by Maelstrom; Apr 19, 2002 at 12:53.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is better I think

    PHP Code:
    <!--ok this should be better-->

    <
    script type="text/javascript">

    date          = new Date();
    hours         date.getHours();
    get_date     date.getDate();
    weekday     date.getDay() + 1;
    month         date.getMonth() + 1;
    year        date.getYear();

    var 
    day   = new Array();

    day[0]    = 'Monday'   ;
    day[1]    = 'Tuesday'  ;
    day[2]    = 'Wednesday';
    day[3]    = 'Thursday' ;
    day[4]    = 'Friday'   ;
    day[5]    = 'Saturday' ;
    day[6]    = 'Sunday'   

    var 
    month = new Array();

    month[0]    = 'January'  ;
    month[1]    = 'February' ;
    month[2]    = 'March'    
    month[3]    = 'April'    ;
    month[4]    = 'may'      ;
    month[5]    = 'june'     ;
    month[6]    = 'july'     ;
    month[7]    = 'august'   ;
    month[8]    = 'september';
    month[9]    = 'october'  ;
    month[10]   = 'november' ;
    month[11]   = 'december' ;

    var 
    quotes = new Array();

    quotes[0]    = ' Good Night '    ;
    quotes[1]    = ' Good Morning '  ;
    quotes[2]    = ' Good Afternoon ';
    quotes[3]    = ' Good Night '    ;
    quotes[4]    = ' Now It Is '     ;

    with (document)
    {
        
        if (
    hours 6)            write(quotes[0]); 
        if (
    hours 12 && hours 6)     write(quotes[1]); 
        if (
    hours 18 && hours 12)     write(quotes[2]);
        else                
    write(quotes[0]);


        
    write('<br /> ' quotes[4] + ' ');
        
    write(hours);
        
        switch (
    weekday)
        {
            case 
    write(' ' day[6]);break;
            case 
    write(' ' day[0]);break;
            case 
    write(' ' day[1]);break;
            case 
    write(' ' day[2]);break;
            case 
    write(' ' day[3]);break;
            case 
    write(' ' day[4]);break;
            case 
    write(' ' day[5]);break;
        }

        
    write(' ' get_date  ' ');

        switch (
    month)
        {
            case 
    1  write(month[0]);break;
            case 
    2  write(month[1]);break;
            case 
    3  write(month[2]);break;
            case 
    4  write(month[3]);break;
            case 
    5  write(month[4]);break;
            case 
    6  write(month[5]);break;
            case 
    7  write(month[6]);break;
            case 
    8  write(month[7]);break;
            case 
    9  write(month[8]);break;
            case 
    10 write(month[9]);break;
            case 
    11 write(month[10]);break;
            case 
    12 write(month[11]);break;
        }

        
    write(' of ' year
    }

    </script> 

  7. #7
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is better form, but for a beginner that is a big jump. But it definately something he should look at learning. Multi-dimentional arrays, conditionals etc...always better form.

    Personally I would take what you did and go one step further and create a variable to contain all of the different parts and then print the variable. Instead of writing as the script runs
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  8. #8
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is better form, but for a beginner that is a big jump. But it definately something he should look at learning. Multi-dimentional arrays, conditionals etc...always better form.
    Reason i didn't use a multi.d-array was exactly because of this, I find them very tedious, to make sure your pulling the right values out of the, so I tend to stick with single arrays. But im curious what you meant by this
    Personally I would take what you did and go one step further and create a variable to contain all of the different parts and then print the variable. Instead of writing as the script runs

  9. #9
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Andrew-J2000
    You simply build the variable as you go..instead of using document.write you keep adding it to a variable

    var bob="test";
    bob += " the power";
    bob += " of javascript";

    document.write(bob);

    I generally prefer doing long drawn out javascripts like this. I can track variables and errors down better for some reason. I even use the same technique in php. It also works really well for errors, which I place into an array. As it tests for error it populates an array which I then use a loop to run the correct number of errors. It allows for more control I find
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  10. #10
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arr k, I see what you mean now.

  11. #11
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A few changes....!

    Since I'm never able to resist the urge to optimise longwinded code and because I'm a little out of touch with JS, I've rewritten the code to tidy it up, fix bugs, etc.


    The changes I have made are as follows:

    1) arrays renamed - very bad practice to have two variables called the same thing (number 'month' and array 'month')!

    2) array code condensed (smaller code, but slightly less readable admittedly), some elements removed from list_quotes[], and rearranged more logically.

    3) corrected capitalisation throughout

    4) corrected and simplified hours->time of day code (due to incorrect use of '>' rather than '>=', 12x came out as night-time)

    5) used string concatenation instead of multiple calls to document.write

    6) rearranged order of days in list_day[] so that 'switch()' statement to get name of weekday could be elminated

    7) eliminated 'switch()' statement to get name of month

    8) corrected time display (didn't make sense previously)


    The altered code is as follows:
    Code:
    <SCRIPT TYPE="text/javascript">
    var date      = new Date();
    var hours     = date.getHours();
    var mins      = date.getMinutes();
    var get_date  = date.getDate();
    var weekday   = date.getDay() + 1;
    var month     = date.getMonth() + 1;
    var year      = date.getYear();
    
    var list_day = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    
    var list_month = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    
    var list_quotes = new Array('Good morning.','Good afternoon.','Good night.');
    
    var msg = "";
    
    switch (true)
    {
    	case (hours >= 6 && hours < 12): msg += list_quotes[0]; break;//morning
    	case (hours >= 12 && hours < 18): msg += list_quotes[1]; break;//evening
    	default: msg += list_quotes[2]; break;//night
    }
    
    msg += '<br />It is now ' + hours + ':' + mins + ' ' + list_day[weekday-1];
    msg += ' ' + get_date  + ' ' + list_month[month-1] + ' of ' + year; 
    document.write(msg);
    </SCRIPT>

    M@rco

  12. #12
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe nice,

  13. #13
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation :), another doubt

    "switch (true)"
    what does it mean ?

    Thank You for all people who answered me,
    i am trying to build a code like M@rco did, but only with a unique var like Maelstorm said (but i donīt know if i understeand it correct :/"
    LOL

  14. #14
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    M@rco used a unique variable to display the output.

    all this

    PHP Code:
    msg += '<br />It is now ' hours ':' mins ' ' list_day[weekday-1];
    msg += ' ' get_date  ' ' list_month[month-1] + ' of ' year
    which he outputs once

    PHP Code:
    document.write(msg); 
    switch (true)

    is just a case statement these sites will explain it
    [list=1][*]w3schools[*]javascriptkit [/list=1]

  15. #15
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Andrew.

    I suspect that what probably threw TheN00B (and perhaps other people) more than anywthing else is the way in which I used the "switch()" statement - most people have never seen code that uses it the way I have here (although it's perfectly valid, and the cleanest solution in many cases such as this). It is of course equally valid and useful for the VBS "Select Case" statement.

    Code:
     ? ___ ? <----(thinking outside the box!)
      |   |      /
      |___|     /
     ?     ? <-/

    M@rco


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •