[thelist] JavaScript: Hours & Minutes

deke web at master.gen.in.us
Sat Mar 10 08:01:28 CST 2001


On 10 Mar 2001, at 9:54, ...::: [ M ] [ a ] [ d ] [ s  wrote:

> hi, i hope you can help me with this part of my script:
> =================================================================
> var hs
> if(hours >= 06 && minutes > 28)&&(hours <09 + minutes >30)
> =================================================================
> the goal for this script, is to make it show witch tv-program that runs
> BETWEEN 06:28 and 09:30, everything else than the "minutes" variabel work.
> It dosen't "care" about the "hours" variabel, and then it only works when my
> clock is between:
> xx:28 and xx:30 =( So... how can i make this out?

Multiply your hours by 60 and add minutes, and use that for
comparison.

You seem to be a beginner at programming, based on your
difficulties. Complicated expressions are hard even for 
programmers who no longer confuse '+' and '&&'

How do you simplify this expression?  

First, create three variables, striketime (built from hours and 
minutes), starttime and endtime, so that the conversion from
hours and minutes to just minutes is in fairly simple statements
isolated from other complexity.

Second, avoid the "greater than or equal two" type operators.
They are an invitation to typos that work fine except they give
the wrong results. Instead of "at or after 6:28", you want to 
think in terms of "after 6:27"

Third, do single comparisons and nest them, rather than doing
complex comparisons.  
    if cherry is red
             if olive is green
                     then use with vanilla ice cream for Christmas sundae
rather than 
     if ((cherry is red) and (olive is green))
                     then use with vanilla ice cream for Christmas sundae
because it's easy to accidently use a plus instead of an and. 
     if ((cherry is red) plus (olive is green))
actually means
     if ((cherry is red) or (olive is green))
in most languages (because false is zero and anything else is
true)  You also avoid all those darned parentheses. They were
hard enough to keep track of when we used ugly fixed width
fonts, but in the proportional fonts of most modern text editors,
parentheses are so thin that they are even harder to keep right.

deke





                    














--------
I got this powdered water. 
Now I don't know what to add. 
            -- Steven Wright




More information about the thelist mailing list