Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Mistake in Calendar and Trying to Find a Way to Not to Choose the Same Random Number

    Hello to the Coding Forums!

    This is my first post!

    I am trying to (re)write a calendar which starts on Monday, but days is one day off. For example, September 1st 2012 shows Sunday instead of Saturday and June 8th 2012 shows Saturday instead of Friday. I couldn't find the mistake myself.

    Code:
     var monthName = new Array("JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER");
    
    function calendar() {
    var calDate = new Date("September 01, 2012");
    document.write("<table id='calendar_table'>");
    writeCalTitle(calDate);
    writeDayTitle();
    writeCalDays(calDate);
    document.write("</table>");
    }
    
    function writeCalTitle(calendarDay) {
    
    var thisMonth = calendarDay.getMonth();
    var thisYear = calendarDay.getFullYear();
    
    document.write("<tr>");
    document.write("<th colspan='7'>");
    document.write("<span id='month'>"+monthName[thisMonth]+" </span><span id='year'>"+thisYear+"</span>");
    document.write("</th>");
    document.write("</tr>");
    }
    
    function writeDayTitle() {
    var dayName = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
    
    document.write("<tr>");
    for (var i=0;i<=4;i++){
    document.write("<th class='calendar_weekdays'>"+dayName[i]+"</th>");
    }
    
    for (var i=5;i<=6;i++){
    document.write("<th class='calendar_weekends'>"+dayName[i]+"</th>");
    }
    document.write("</tr>");
    }
    
    function daysInMonth (calendarDay) {
    var dayCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
    var thisYear = calendarDay.getFullYear();
    var thisMonth = calendarDay.getMonth();
    if(thisYear%4==0){
    if((thisYear%100!=0)||(thisYear%400==0)){
    dayCount[1]=29;
    }
    }
    return dayCount[thisMonth];
    }
    
    function writeCalDays(calendarDay){
    var currentDay = calendarDay.getDate();
    var totalDays = daysInMonth(calendarDay);
    calendarDay.setDate(1);
    var weekDay=calendarDay.getDay();
    document.write("<tr>");
    for (var i=0;i<weekDay;i++){
    document.write("<td></td>");
    }
    var dayCount=1;
    while(dayCount<=totalDays){
    if (weekDay==0){
    document.write("<tr>");
    }
    if(dayCount==currentDay){
    document.write("<td class='calendar_dates' id='calendar_today'>"+dayCount+"</td>");
    } else {
    
    document.write("<td class='calendar_dates'>"+dayCount+"</td>");
    }
    if (weekDay==6){
    document.write("</tr>");
    }
    dayCount++;
    calendarDay.setDate(dayCount);
    weekDay=calendarDay.getDay();
    }
    document.write("</tr>");
    }
    My second question is I am trying to use 11 values from an array of 11 items, but I do want items to be changed at every load, but never be the same at a time (var1!=var2!=!var3...). I am new to javascript and couldn't find the right function to do that. I am just posting the related codes.

    Code:
    var colors = new Array("aqua","blue","fuchsia","lime","magenta","navy","purple","red","white","yellow","black");
    var color1 = randomInteger(10);
    var color2 = randomInteger(10);
    var color3 = randomInteger(10);
    var color4 = randomInteger(10);
    var color5 = randomInteger(9);
    var color6 = randomInteger(10);
    var color7 = randomInteger(10);
    var color8 = randomInteger(10);
    var color9 = randomInteger(10);
    var color10 = randomInteger(10);
    var color11 = randomInteger(10);
    
    function randomInteger(size) {
    return Math.floor((size+1)*Math.random());
    }
    
    function changeColor() {
    
    document.write("h1 {color:"+colors[color1]+"}");
    
    document.write("h2 {color:"+colors[color2]+"}");
    
    document.write("#month {color:"+colors[color3]+"}");
    
    document.write("#year {color:"+colors[color4]+"}");
    
    document.write(".calendar_dates {color:"+colors[color5]+"}");
    
    document.write(".calendar_weekdays {color:"+colors[color6]+"}");
    
    document.write(".calendar_weekends {color:"+colors[color7]+"}");
    
    document.write("#calendar_today {color:"+colors[color8]+"}");
    
    document.write("#clock {color:"+colors[color9]+"}");
    
    document.write("#clock p {color:"+colors[color10]+"}");
    
    document.write("#clock input {color:"+colors[color11]+"}");
    }
    Now, instead of creating 11 variables, is there any other way that I can use and how can I make it in a way that the colors will be random and never be the same at a time.

    color1!=color2!=color3!=color4....

    My main question is do I have to create 11 variables or not, the second part is less important.

    P.S.: color 5 is randomInteger(9) on purpose; I didn't want it to be black, but if there is an answer to my question but color5 has to be randomInteger(10) instead of randomInteger(9); it is absolutely ok.

    Thank you.
    Last edited by IMayNeed; 06-09-2012 at 12:27 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Change this line to
    Code:
    var dayName = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    Day 0 is Sunday, Day 6 is Saturday.

    To avoid repetition you need to shuffle your colors into a new order:-

    Code:
    <script type="text/javascript">
    
    Array.prototype.shuffle = function() {
    var s = [];
    while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
    while (s.length) this.push(s.pop());
    return this;
    }
    
    var colors = new Array("aqua","blue","fuchsia","lime","magenta","navy","purple","red","white","yellow","black");
    colors.shuffle();
    
    alert (colors);  // the shuffled array
    
    for (var i =0; i<colors.length; i++) {
    alert (colors[i]);
    }
    
    </script>
    As has often been stated in this forum, document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. Use DOM methods instead.

    I am new to java..
    Be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!


    "Let us be thankful for the fools. But for them the rest of us could not succeed. " - Mark Twain, US humorist, novelist, short story author, & wit (1835 - 1910)
    Last edited by Philip M; 06-08-2012 at 07:50 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    IMayNeed (06-09-2012)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by iBall View Post
    <offTopic style="Tip">

    In that case using document.write() is fine for this purpose to help students quickly get output to the browser while learning the basics of javascript.

    As soon as you start learning more about the Document Object Model (DOM), you really should then use more appropriate DOM methods to output to the browser.

    document.write outputs to the current page only while it is still loading. If called after the the page has loaded, it will output to a new page. document.write is really only useful for writing to child pages.

    </offTopic>
    Using innerHTML is far more straightforward and less error prone for beginners to use than document.write is - there's no reason for using the more complicated document.write rather than innerHTML except in those advanced userscripts where document.write is the only way that works.

    There are far fewer situations where innerHTML will break the web page functionality compared to the ways that document.write can break the functionality.

    Of course the way that always works is to use the proper DOM commands that make up about 60% of JavaScript but learning those is a bit more complex and since innerHTML will work most of the time it is the obvious choice for beginners to use first.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for the shuffle suggestions.

    When I make the days array start with Sunday, how can I change the code to create a monthly calendar that starts with Monday?

    Yes, I am a student. And, the only internet I have is at school so most of the time, I have to use books and downloaded scripts to work.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by IMayNeed View Post
    When I make the days array start with Sunday, how can I change the code to create a monthly calendar that starts with Monday?
    See http://javascript.about.com/library/blweekstart.htm
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by IMayNeed View Post
    I am trying to (re)write a calendar which starts on Monday, but days is one day off. For example, September 1st 2012 shows Sunday instead of Saturday and June 8th 2012 shows Saturday instead of Friday.
    To find the day of the week for any date, use Zeller's Congruence ( LINK ).

    My implementation in Javascript:

    Code:
    /***
     * zeller's congruence
     * returns day of week (0=Sunday, 1=Monday, etc...)
     ***/
    function zeller (day, month, year) {
        var dow = -1;
        if (3 > month) {
            month += 12;
            year -= 1;
        }
        dow += day;
        dow += parseInt(((month + 1) * 26) / 10);
        dow += year;
        dow += parseInt(year / 4);
        dow += (6 * parseInt(year / 100));
        dow += parseInt(year / 400);
        return dow % 7;
    };
    Hope this is of some help to you.......
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    IMayNeed (06-09-2012)

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Krupski View Post
    To find the day of the week for any date, use Zeller's Congruence ( LINK ).

    My implementation in Javascript:

    Code:
    /***
     * zeller's congruence
     * returns day of week (0=Sunday, 1=Monday, etc...)
     ***/
    function zeller (day, month, year) {
        var dow = -1;
        if (3 > month) {
            month += 12;
            year -= 1;
        }
        dow += day;
        dow += parseInt(((month + 1) * 26) / 10);
        dow += year;
        dow += parseInt(year / 4);
        dow += (6 * parseInt(year / 100));
        dow += parseInt(year / 400);
        return dow % 7;
    };
    Hope this is of some help to you.......
    It is far easier to just use the built in date object - load the date and retrieve the day. All you need to do to get the day of the week with Monday as 0 and Sunday as 6 is to use:

    Code:
    Date.prototype.mGetDay = function() {
     return (this.getDay() + 6) %7;
     }
    dayOfWeek = new Date(year, month-1, day).mGetDay();
    Also you are misusing parseInt in that code where you should be using Number() (which is really only needed around the (month+1) part if you are passing the month astext instead of as a number - you don't need to convert to base 10 anywhere in that function and so it doesn't need parseInt at all.
    Last edited by felgall; 06-09-2012 at 02:01 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    IMayNeed (06-09-2012)

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by felgall View Post
    Also you are misusing parseInt in that code where you should be using Number() (which is really only needed around the (month+1) part if you are passing the month astext instead of as a number - you don't need to convert to base 10 anywhere in that function and so it doesn't need parseInt at all.
    That code snippet is just a standard algorithm I use for my own stuff here. I have the same code in C, Intel ASM, Mototola ASM, GWBASIC and Javascript.

    I do need parseInt because the algorithm depends on integer math and indeed fails if floating point numbers get involved.

    If, for example, part of the algorithm does "1987 / 4", the result (496.75) would propagate through and produce an incorrect day of week. So I have to force JS to act as though all numbers are (INT).

    For fun, make an array of day names, then run the JS code and plug in well known dates (Dec 7, 1941 - Sep 11, 2001, - etc...) and see what happens with and without "parseInt".

    Edit:
    How's this for some wild code?

    Code:
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; clockset.asm - sets smartclocks
    ; (c) 1996, 2010 roger a. krupski
    ; Thu  01/14/2010 17:31:45
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    clock   equ     $a000           ;clock memory mapped address
    
    ; offsets into clkbuf (smartclock data - see below)
    
    centi   equ     0               ;centiseconds offset
    secs    equ     1               ;seconds offset
    mins    equ     2               ;minutes offset
    hours   equ     3               ;hours offset
    day     equ     4               ;osc/rst/day offset
    date    equ     5               ;date offset
    month   equ     6               ;month offset
    year    equ     7               ;year offset
    
    regbs   equ     $1000           ;register base
    scsr    equ     regbs+$2e       ;sci status reg
    scdr    equ     regbs+$2f       ;sci data reg
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; program starts here
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
            org     $2000           ;program in sram
    
            jmp     start           ;jump around smartclock driver
    
    ;        include m4859.asm       ;driver for st micro
    ;        include ds-1215.asm     ;driver for smart-ram
            include ds-1216.asm     ;driver for smart-rom
    
    
    start   ldaa    #' '            ;a = space
            ldab    #bufsiz         ;buffer size
            ldx     #buffer         ;point to input buffer
    
    clrbuf  staa    0,x             ;erase buffer
            inx                     ;bump pointer
            decb                    ;count down
            bne     clrbuf          ;do the rest
    
            clrb                    ;b=0
            stab    curpos          ;init cursor position to 0
    
            ldx     #msg1           ;point to prompt
            jsr     outstrg         ;display it
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; this is the main user input loop
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    main    jsr     input           ;read serial term
            beq     main            ;wait for a key
    
            cmpa    #3              ;ctrl-c?
            beq     exit            ;yes, exit
            cmpa    #27             ;escape?
            bne     main2           ;no, run program
    
    exit    ldx     #msg3           ;point to abort message
            jsr     outstrg         ;print it
    
            swi                     ;ctrl-c received, exit!
    
    
    main2   cmpa    #13             ;c/r?
            beq     setit           ;yes, set the clock!
    
            cmpa    #8              ;backspace?
            beq     do_bs           ;yes, do it
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; here all controlling characters have been tested
    ; for. now we look for valid input such as spaces
    ; and numbers 0 thru 9. if not, we just ignore them.
    ; if matched, placed in the buffer.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
            cmpa    #' '            ;space?
            beq     char            ;yes, add to buffer
    
            cmpa    #'0'            ;number?
            blo     main            ;no, ignore
            cmpa    #'9'            ;number?
            bhi     main            ;no, ignore
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; legal character found, write it to buffer
    ; legal character is either a space or 0 thru 9
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    char    ldx     #buffer         ;point to buffer
            ldab    curpos          ;get cursor position
            cmpb    #prlen          ;at end?
            bhs     chrout          ;at end, can't do anything
    
            abx                     ;x points to current char
            staa    0,x             ;write char to buffer
    
            incb                    ;move fwd 1 position
            stab    curpos          ;update cursor position
    
            jsr     output          ;echo char to term
    
    chrout  jmp     main            ;return to main loop
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; do backspace
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    do_bs   ldx     #buffer         ;point to buffer
            ldab    curpos          ;get cursor position
            beq     dobsout         ;at beginning, can't do anything
    
            decb                    ;backup 1 position
            stab    curpos          ;update cursor position
    
            abx                     ;x points -> current char in buffer
    
            ldaa    #' '            ;a = space
            staa    0,x             ;erase char in buffer
    
            ldaa    #8              ;load backspace
            jsr     output          ;print backspace to term
    
            ldaa    #' '            ;load a space
            jsr     output          ;erase char on term
    
            ldaa    #8              ;load backspace
            jsr     output          ;backup cursor on term
    
    dobsout jmp     main            ;return to main loop
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; done getting input, attempt to set the clock!
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    setit   ldx     #buffer+hro     ;hour input
            ldy     #clkbuf+hours   ;hour for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #23             ;legal?
            bhi     badval          ;no, abort!
    
            ldx     #buffer+mio     ;minutes input
            ldy     #clkbuf+mins    ;minutes for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #59             ;legal?
            bhi     badval          ;no, abort!
    
            ldx     #buffer+sco     ;seconds input
            ldy     #clkbuf+secs    ;seconds for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #59             ;legal?
            bhi     badval          ;no, abort!
    
            ldy     #clkbuf+centi   ;centi for clock
            clr     0,y             ;default to zero
    
            ldx     #buffer+dao     ;date input
            ldy     #clkbuf+date    ;date for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #1              ;legal?
            blo     badval          ;no, abort
            cmpb    #31             ;legal?
            bhi     badval          ;no, abort!
    
            ldx     #buffer+mno     ;month input
            ldy     #clkbuf+month   ;month for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #1              ;legal?
            blo     badval          ;no, abort
            cmpb    #12             ;legal?
            bhi     badval          ;no, abort!
    
            ldx     #buffer+yro     ;year input (last 2 digits)
            ldy     #clkbuf+year    ;year for clock
            jsr     conv            ;convert 2 ascii to 1 packed bcd
            cmpb    #99             ;legal?
            bhi     badval          ;no, abort!
    
            jsr     dayweek         ;get day of week (0=sunday)
            andb    #%00000111      ;strip unused bits
            orab    #%00010000      ;"or" in osc enable for dallas
            stab    clkbuf+day      ;set osc, reset and d.o.w.
    
            bra     setclk          ;set clock clock & swi
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; arrive here if invalid data (like "hour 90" is illegal)
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    badval  ldx     #msg4           ;bad value
            jsr     outstrg         ;print it
    
            swi                     ;return to monitor
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; actually set the clock from the buffer here
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    setclk  ldx     #clkbuf         ;point to buffer
            ldy     #clock          ;point to clock chip
    
            ldaa    #0
            jsr     doclk           ;write time to clock
    
            ldx     #verbuf         ;point to verify buffer
            ldy     #clock          ;point to clock chip
    
            ldaa    #1
            jsr     doclk           ;read time back
    
            ldx     #msg2           ;"data verify...."
            jsr     outstrg         ;print it
    
            ldx     #clkbuf         ;point to write buffer
            ldy     #verbuf         ;point to read buffer
            ldab    #8              ;bytes to verify
    
    verf    cpx     #clkbuf+centi   ;the "centiseconds" byte?
            beq     verf2           ;yes, skip test
    
            cpx     #clkbuf+day     ;the "day" byte?
            beq     verf2           ;yes, skip test
    
            ldaa    0,x             ;get a byte
            cmpa    0,y             ;match?
            bne     fail            ;yes
    
    verf2   inx                     ;bump...
            iny                     ;...pointers
            decb                    ;count down
            bne     verf            ;check rest
    
            ldx     #msg2s          ;"...success!"
    showit  jsr     outstrg         ;print it
    
            swi                     ;return to buffalo
         
    fail    ldx     #msg2f          ;"...failed!"
            bra     showit          ;print it
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; convert 2 ascii bytes to one packed bcd number
    ; for the clock and also to one hex number
    ; for range validation.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    conv    psha
            pshx                    ;save registers
    
            ldab    0,x             ;get hi byte
            cmpb    #' '            ;space?
            bne     conv2           ;no, leave it
    
            ldab    #'0'            ;yes, convert to zero
    
    conv2   subb    #'0'            ;ascii -> decimal
    
            aslb                    ;slide up to...
            aslb
            aslb
            aslb                    ;...hi nybble
    
            ldaa    1,x             ;get lo byte
            cmpa    #' '            ;space?
            bne     conv3           ;no, leave it
    
            ldaa    #'0'            ;yes, convert to zero
    
    conv3   suba    #'0'            ;ascii -> decimal
    
            aba                     ;a+b->a
            tab                     ;a->b
            stab    0,y             ;store to clock buffer
    
            bsr     bcd2dec         ;convert bcd to decimal
    
            pulx
            pula                    ;restore registers
    
            rts                     ;return with decimal in b
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; packed bcd to decimal converter
    ;
    ; entry: b = bcd number 00-99
    ; exit : b = decimal equivalent
    ; example: entry b=$50, exit b=$32 (decimal 50)
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    bcd2dec psha                    ;save a
            pshb                    ;b on stack
    
            lsrb                    ;shift hi...
            lsrb                    ;...nibble down...
            lsrb                    ;...to lower...
            lsrb                    ;...nibble
    
            ldaa    #6              ;a=6
            mul                     ;make conversion factor
            negb                    ;make factor negative
            pula                    ;restore b into a
            aba                     ;add negative = subtract positive
            tab                     ;a->b (result to b)
            pula                    ;restore a
            rts                     ;return to caller
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; here we find the day of week as 0 thru 6
    ; 0 thru 6 where 0=sunday, 1=monday, etc...
    ; using dr. christian zeller's congruence.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    dayweek ldx     #clkbuf         ;point to raw clock data
    
            ldab    month,x         ;get bcd month
            jsr     bcd2dec         ;convert to decimal
            pshb                    ;month->stack
    
            ldab    date,x          ;get bcd day
            jsr     bcd2dec         ;convert to decimal
            pshb                    ;day->stack
    
            clra                    ;zero top of d
            ldab    year,x          ;get bcd year
            jsr     bcd2dec         ;convert to decimal
            addd    #2000           ;convert to full year
            pshb
            psha                    ;year->stack
    
            pulx                    ;year->x
            pulb                    ;day->b
            pula                    ;month->a
    
    ;; fall through to zeller, then rts with data
    
    ;; stack offsets
    
            offset  0
    
    mn      ds.w    1               ;temp month
    dy      ds.w    1               ;temp day
    yr      ds.w    1               ;temp year
    tmp     ds.w    1               ;intermediate results
    ssiz    equ     *-mn            ;storage size
    
    astack  ds.b    1               ;incoming month
    bstack  ds.b    1               ;incoming day / outgoing result
    xstack  ds.w    1               ;incoming year
    ystack  ds.w    1               ;just saved
    
    
    ;;;;;;;;;;;;;;;;;;;; code starts here ;;;;;;;;;;;;;;;;;;;;;;
    
    zeller  pshy
            pshx
            pshb                    ;(*) accb will get modified
            psha                    ;save registers
    
    ;; allocate temp storage on the stack
    
            ldab    #ssiz           ;storage size
    alloc   des                     ;reserve stack
            decb                    ;more?
            bne     alloc           ;yes do rest
    
            tsy                     ;stack -> y (points to temp storage)
    
            ldd     #0              ;d=0
            std     tmp,y           ;initialize accumulator
    
            ldab    astack,y        ;get month lo byte
            std     mn,y            ;store month (a=0)
    
            ldab    bstack,y        ;get day lo byte
            std     dy,y            ;store day (a=0)
    
            ldd     xstack,y        ;get year
            std     yr,y            ;store year (word)
    
    ;; this does "month 13 and 14" correction
    
            ldd     mn,y            ;get month
            cpd     #2              ;jan or feb?
            bhi     zel2            ;no
    
            addd    #12             ;yes, add a year
            std     mn,y            ;store updated month
            ldd     yr,y            ;get year
            subd    #1              ;correct year (yr=yr-1)
            std     yr,y            ;store updated year
    
    zel2    ldd     mn,y            ;m
            addd    #1              ;(m+1)
            ldaa    #26             ;26
            mul                     ;d=(m+1)*26
            ldx     #10             ;10
            idiv                    ;x=((m+1)*26)/10
            xgdx                    ;x->d
            addd    tmp,y           ;do f1
            std     tmp,y           ;store intermediate
    
            ldd     yr,y            ;year
            ldx     #4              ;4
            idiv                    ;x=(year/4)
            xgdx                    ;x->d
            addd    tmp,y           ;do f2
            std     tmp,y           ;store intermediate
    
            ldd     yr,y            ;year
            ldx     #100            ;100
            idiv                    ;x=(year/100)
            xgdx                    ;x->d
            ldaa    #6              ;a=6
            mul                     ;d=6*(year/100)
            addd    tmp,y           ;do f3
            std     tmp,y           ;store intermediate
    
            ldd     yr,y            ;year
            ldx     #400            ;400
            idiv                    ;x=(year/400)
            xgdx                    ;x->d
            addd    tmp,y           ;do f4
            std     tmp,y           ;store intermediate
    
            ldd     dy,y            ;get day
            addd    tmp,y           ;add intermediates
            addd    yr,y            ;add year
            subd    #1              ;-1 (make sunday=0)
            ldx     #7              ;mod 7
            idiv                    ;x=d/x, quotient to x, remainder to d
    
            stab    bstack,y        ;(*) store result, return in accb
    
    ;; deallocate storage on the stack
    
            ldab    #ssiz           ;storage size
    dealloc ins                     ;deallocate
            decb                    ;more?
            bne     dealloc         ;yes do rest
    
    ;; restore registers
    
            pula
            pulb                    ;(*) day of week was placed here
            pulx
            puly                    ;restore registers
    
            rts                     ;return with day of week in b
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; input (read) data from serial port
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    input   ldaa    scsr            ;read status reg
            anda    #%00100000      ;check rdrf flag
            beq     input2          ;exit w/a=0 if no data
    
            ldaa    scdr            ;read serial data
    
    input2  rts                     ;return
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; output (write) data to serial port
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    output  psha                    ;save a
            ldaa    scsr            ;read sci status register
            coma                    ;invert bits
            bita    #%11000000      ;test tdre & tc
            pula                    ;restore a
            bne     output          ;sci busy, check again
    
            staa    scdr            ;send character
            rts
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; out string (print) string to serial port
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    outstrg ldaa    0,x             ;get a char
            beq     outs2           ;done
    
            bsr     output          ;send character out
            inx                     ;increment buffer ptr
            bra     outstrg         ;send next
    
    outs2   rts                     ;return
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; text messages for terminal
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    msg1    dc.b    $0d,$0a
            dc.b    'ENTER TIME & DATE:'
            dc.b    $0d,$0a
    
    hro     equ     *-prompt        ;offset
    prompt  dc.b    'HH '           ;hours prompt
    
    mio     equ     *-prompt
            dc.b    'MM '           ;minutes prompt
    
    sco     equ     *-prompt
            dc.b    'SS '           ;seconds prompt
    
    mno     equ     *-prompt
            dc.b    'MM '           ;month prompt
    
    dao     equ     *-prompt
            dc.b    'DD '           ;date prompt
    
    yro     equ     *-prompt
            dc.b    'YY'            ;year prompt
    
    prlen   equ     *-prompt        ;prompt length (for input limit)
    crlf    dc.b    $0d,$0a,0
    
    msg2    dc.b    $0d,$0a
            dc.b    'READBACK ',0
    
    msg2s   dc.b    'CORRECT!'
            dc.b    $0d,$0a,0
    
    msg2f   dc.b    'FAILED!'
            dc.b    $0d,$0a,0
    
    msg3    dc.b    $0d,$0a
            dc.b    'CANCELLED BY USER',0
    
    msg4    dc.b    $0d,$0a
            dc.b    'DATA OUT OF RANGE'
            dc.b    $0d,$0a,0
    
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; buffers & vars in 'hc11 sram
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
            org     $100            ;evbu ram
    
    buffer  ds.b    32              ;incoming data buffer
    bufsiz  equ     *-buffer        ;size of buffer
    curpos  ds.b    1               ;cursor position
    clkbuf  ds.b    8               ;data to be fed to clock
    verbuf  ds.b    8               ;clock verify buffer
    
            end
    Last edited by Krupski; 06-09-2012 at 02:48 AM.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    IMayNeed (06-09-2012)

  • #9
    New Coder
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you for the help everybody. I am copying this whole page and getting home. Today, I just got out of 2 finals so, I will rest probably, but Weekend, I will finish this calendar and the rest of the site (one transaction, one credit card, one comment forms).

    I have to ask this too (after checking out books mostly -don't have internet at home-); I have a frame (row) at the top and when I make the window smaller, the text disappears. Is there any way to avoid that or with the right words, what are the ways to avoid that? I checked out the books I had, but either I am not getting it or I am having a hard time to get it or something else!

    Thanks again.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Krupski View Post
    I do need parseInt because the algorithm depends on integer math and indeed fails if floating point numbers get involved.
    but parseInt() in JavaScript doesn't convert the values to integers, it converts between number bases eg base 16 to base 10

    Math.floor() is the JavaScript function to convert from a float to an integer.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by felgall View Post
    but parseInt() in JavaScript doesn't convert the values to integers, it converts between number bases eg base 16 to base 10

    Math.floor() is the JavaScript function to convert from a float to an integer.
    We are like "Rock'em-Sock'em Robots"... trying to knock off each other's block.

    parseInt defaults to base 10 so it does the same thing as Math.floor.

    As to which one is "better"... does it matter? Do you eat the cake first, or the frosting first? And does it matter either way?

    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Krupski View Post
    parseInt defaults to base 10 so it does the same thing as Math.floor.
    parseInt doesn't always default to base 10 - sometimes it defaults to base 8.


    The difference between using the function intended to convert to integer Math.floor(x) and one that does so as a side effect but which is actually intended for a different purpose parseInt(x,10) is the same difference as there is between driving a Ford to work or driving a Cesna to work - one is intended for that purpose and the other is intended to be used for something different.

    Just watch out that you don't get your wings knocked off next time you drive your Cesna to work.

    In particular parseInt should NEVER be used where dates are concerned because of the danger that it will treat numbers as base 8 instead of base 10 and so will fail to work on the 8th and ninth of each month as well as failing completely for August and September. I have lost count of the number of forum questions I have answered where this problem occurred because someone misused parseInt.

    Anyway that code you posted with 15 lines in the function is about 13 lines longer than necessary as I showed in my following post where including the actual call to the function my solution used only 3 lines of code to do the same thing.
    Last edited by felgall; 06-09-2012 at 04:59 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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