By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

looking for a javascript calendar...

P: n/a
preferably one that when clicked can update three date dropdowns (day,
month, year) like

http://www.visitdublin.com/carhire/avis.asp

Don't mind paying for the file... anyone seen something like this?
--

"I hear ma train a comin'
.... hear freedom comin"
Jul 23 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Why can't you view the source/JS of that page?

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
preferably one that when clicked can update three date dropdowns (day,
month, year) like

http://www.visitdublin.com/carhire/avis.asp

Don't mind paying for the file... anyone seen something like this?
--

"I hear ma train a comin'
... hear freedom comin"

Jul 23 '05 #2

P: n/a
yeah I looked at that, but for some reason I couldn't get it to work on
my localhost.

Why can't you view the source/JS of that page?

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
preferably one that when clicked can update three date dropdowns (day,
month, year) like

http://www.visitdublin.com/carhire/avis.asp

Don't mind paying for the file... anyone seen something like this?
--

"I hear ma train a comin'
... hear freedom comin"


--

"I hear ma train a comin'
.... hear freedom comin"
Jul 23 '05 #3

P: n/a
Well there are plenty of places on the web to grab JavaScript calendars
from.

http://www.calendarzone.com/Interactive/
http://javascript.internet.com/calendars/

Personally I prefer the ones that *don't* spawn a new page. Any one of them
could easily be modified to populate the 3 dropdowns that you want.

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
yeah I looked at that, but for some reason I couldn't get it to work on
my localhost.

Why can't you view the source/JS of that page?

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
preferably one that when clicked can update three date dropdowns (day,
month, year) like

http://www.visitdublin.com/carhire/avis.asp

Don't mind paying for the file... anyone seen something like this?
--

"I hear ma train a comin'
... hear freedom comin"


--

"I hear ma train a comin'
... hear freedom comin"

Jul 23 '05 #4

P: n/a
On Tue, 15 Feb 2005 Jarmo <ja***@jarmo.com> wrote:
Well there are plenty of places on the web to grab JavaScript calendars
from.

http://www.calendarzone.com/Interactive/
http://javascript.internet.com/calendars/

Personally I prefer the ones that *don't* spawn a new page. Any one of them
could easily be modified to populate the 3 dropdowns that you want.


I've seen a lot that output the clicked date to a text box like:

http://www.softcomplex.com/products/...dar/demo1.html

I'm pretty much a newb at javascript so would there be a way to use this
method to update 3 dropdowns.

i.e. Instead of a text box, use a hidden field, and set the value of the
hidden field when I click on the javascript calendar (instead of
outputting the text value of the date as is currently done)

Then once the the hidden field value changes it would parse the value in
the hidden field into three separate values and set the selected value
of the dropdowns accordingly.

Would this idea work? Could you give me some pointers? :)

Thanks!
--

"I hear ma train a comin'
.... hear freedom comin"
Jul 23 '05 #5

P: n/a
Yes that would work. Assuming that you're happy to take apart the calendar
code, I'd re-populate the dropdowns from the calendar code where it
currently pushes the selected date into that edit field.

To select the appropriate values from the dropdowns use a bit of code like
this:

function selectOption(optionset, value)
{
for (var ii = 0; ii < optionset.options.length; ii++)
{
if (optionset.options[ii].value == value)
{
optionset.options[ii].selected = true;
break;
}
}
}

<select name=month>
<option value=1>January
<option value=12>December
</select>

selectOption(month, "12");

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
On Tue, 15 Feb 2005 Jarmo <ja***@jarmo.com> wrote:

http://www.softcomplex.com/products/...dar/demo1.html
I'm pretty much a newb at javascript so would there be a way to use this
method to update 3 dropdowns.

i.e. Instead of a text box, use a hidden field, and set the value of the
hidden field when I click on the javascript calendar (instead of
outputting the text value of the date as is currently done)

Then once the the hidden field value changes it would parse the value in
the hidden field into three separate values and set the selected value
of the dropdowns accordingly.

Would this idea work? Could you give me some pointers? :)

Thanks!

Jul 23 '05 #6

P: n/a
On Tue, 15 Feb 2005 Jarmo <ja***@jarmo.com> wrote:
Yes that would work. Assuming that you're happy to take apart the calendar
code, I'd re-populate the dropdowns from the calendar code where it
currently pushes the selected date into that edit field.

To select the appropriate values from the dropdowns use a bit of code like
this:

function selectOption(optionset, value)
{
for (var ii = 0; ii < optionset.options.length; ii++)
{
if (optionset.options[ii].value == value)
{
optionset.options[ii].selected = true;
break;
}
}
}

<select name=month>
<option value=1>January
<option value=12>December
</select>

selectOption(month, "12");


great I'll try that tommorrow. Cheers!

--

"I hear ma train a comin'
.... hear freedom comin"
Jul 23 '05 #7

P: n/a
That should read :

- selectOption(document.forms[0].month, "12"), or
- selectOption(document.myform.month, "12")

as appropriate.

"Stimp" <re*@spumco.com> wrote in message
news:sl****************@carbon.redbrick.dcu.ie...
On Tue, 15 Feb 2005 Jarmo <ja***@jarmo.com> wrote:

selectOption(month, "12");

Jul 23 '05 #8

P: n/a
Stimp wrote:
preferably one that when clicked can update three date dropdowns (day,
month, year)


I'd use <http://www.dynarch.com/projects/calendar/>, but have it update a
regular text input, and couple that with Date::Parse
<http://search.cpan.org/~gbarr/TimeDate-1.16/lib/Date/Parse.pm> to allow
users to enter freeform dates.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #9

P: n/a
Stimp wrote:
preferably one that when clicked can update three date dropdowns (day,
month, year) like
http://www.visitdublin.com/carhire/avis.asp


You can check out mine, which has an example of updating 3 fields just like
at your URL:
http://www.JavascriptToolbox.com/calendarpopup/

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #10

P: n/a
Matt Kruse wrote:
Stimp wrote:
preferably one that when clicked can update three date dropdowns (day,
month, year) like
http://www.visitdublin.com/carhire/avis.asp

You can check out mine, which has an example of updating 3 fields just like
at your URL:
http://www.JavascriptToolbox.com/calendarpopup/


Crikey Matt, is that really "The Simplest Possible
Implementation Of A Calendar Popup" ?

For the record, here's one that, to me, is considerably simpler
- but I make no claims regarding "simplest possible"!!

Oh, it doesn't use a popup...

--
Rob

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Calendar</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
body {font-family: sans-serif;}
..mainDiv
{text-align: center; border: 1px solid red;
width: 14em; height: 17em; position: absolute;
top:4em; left: 1em;
}
..dateHolder
{text-align: center; /* border: 1px solid blue; */
width: 14em; height: 12em; position: absolute;
top:5em; left: 0em;
}
..dateLabel, .monthLabel
{width: 2em; height: 1em; display: block;
position: absolute; top: 4em;
}
..monthLabel
{width: 10em; top: 0em; left: 2em;
}
..dateCell, .weCell, .nodateCell
{text-align: center; border-top: 1px solid white;
border-left: 1px solid white; font-weight: normal;
width: 2em; height: 2em; position: absolute;
line-height: 2; background-color: #333366; color: #ffffff;
}
.weCell
{background-color: #445577; font-weight: normal;}
.nodateCell
{background-color: #ddddff; display: none;}
</style>

<script type="text/javascript">
var startYear = '',
startMonth = '',
selectedYear,
selectedMonth;
var months = ['January','February','March','April','May','June',
'July','August','September','October','November',' December'];

/*************************
Puts dates in the date cells
*************************/
function genDates(ref,yr,mon,t) {
// Calc day number of 1st day of month &
// first cell to put a date in
var refDate= new Date(yr,mon,1);
var startCell = refDate.getDay()-1;
if (startCell < 0) startCell = 6;

var cellDate = 0 - startCell;
var daysInMonth = getMonthDays(yr,mon+1);

document.getElementById('monthLabel').innerHTML =
months[mon] + ' ' + yr;

// If ref cell has kids, then just update
if ( ref.firstChild
&& ref.firstChild.nodeName.toLowerCase() == 'div') {
currentCell = ref.firstChild;
var cellCount = 0;
cellDate++;
// Assign different classNames to dates
while (currentCell) {
// If there's no date for this cell
if (cellDate <= 0 || cellDate > daysInMonth) {
currentCell.className = 'nodateCell';
// If it's Saturday or Sunday
} else if ( cellCount%7-5 == 0 || cellCount%7-6 == 0) {
currentCell.className = 'weCell';
// Must be a week day
} else {
currentCell.className = 'dateCell';
}
currentCell.firstChild.data = cellDate;
cellCount++ ;
cellDate++ ;
currentCell = currentCell.nextSibling;
}
return;
}

// Otherwise, build table from scratch
// Only runs when first created (onload)
var cellWidth = '2',
cellHeight = '2',
cellUnit = 'em',
lineHeight = '2';
clearKids(ref);
for (var i=0; i<6; i++) {
for (var j=0; j<7; j++) {
var oDiv = document.createElement('div');
oDiv.style.width = cellWidth + cellUnit;
oDiv.style.height = cellHeight + cellUnit;
oDiv.style.left = j * cellWidth + cellUnit;
oDiv.style.top = i * cellHeight + cellUnit;
oDiv.style.lineHeight = lineHeight;
// Assign class for no date, weekend, week as above
if (cellDate < 0 || cellDate >= daysInMonth) {
oDiv.className = 'nodateCell';
} else if ( j == 5 || j == 6) {
oDiv.className = 'weCell';
} else {
oDiv.className = 'dateCell';
}
cellDate++;
oDiv.appendChild(document.createTextNode(cellDate) );
oDiv.onclick = sayDate;
ref.appendChild(oDiv);
}
}
}

/*************************
Removes all child nodes
*************************/
function clearKids(ref){
while(ref.firstChild){
ref.removeChild(ref.firstChild);
}
}

/*************************
Builds the calendar
*************************/
function initCal(yr,mon) {
var now = new Date();

// If have been passed a month, assume also got a year
if (typeof mon == 'number') {
now.setMonth(mon);
now.setYear(yr);

// Otherwise, declare mon & yr and set selectors
} else {
var mon = now.getMonth();
var yr = now.getFullYear();
}

// If not initialising from initCal, put selector
// setters inside above else statement.
// Add options to month select
var y = document.dateSel.monthSel;
// If options don't exist, build 'em
var len = months.length;
if (y.length != len) {
y.length = len;
for (var i=0; i<len; i++) {
y[i].text = months[i];
}
}
// Select the current month
y.selectedIndex = mon;

// Add years to year selector (current year +-5)
// Select the current year
var yrRange = 6;
var z = document.dateSel.yearSel;
len = yrRange*2 + 1;
if (z.length != len) {
z.length = len;
var t = yr - yrRange;
for (var k=0; k<len; k++){
z[k].text = t;
if (z[k].text == yr) z[k].selected = true;
t++;
}
} else {
for (var k=0; k<len; k++){
if (z[k].text == yr) {
z[k].selected = true;
}
}
}

if (startYear == '') startYear = yr;
if (startMonth == '') startMonth = mon;
selectedYear = yr; // Remember year & month selected
selectedMonth = mon; // as global variables

if (document.getElementById) {
genDates(document.getElementById('dateBlock'),yr,m on);
}
}

/*************************
Returns the number of days in the month,
given year and month (1-12) numbers
*************************/
function getMonthDays(Y,M){
if ( M==4 || M==6 || M==9 || M==10){
return 30;
} else if ( M==2 && Y%4==0){
if ( Y%100!=0 || Y%400==0){
return 29;
}
} else if ( M==2 ) {
return 28;
}
return 31;
}

/*************************
Sets the calendar back to the initial month
and resets the form (which is pretty pointless...)
*************************/
function resetForm(){
document.dateSel.reset();
if (startYear != '' && startMonth != '')
initCal(startYear,startMonth);
}

/*************************
Does something when a cell is clicked on
Could return the date to a form
*************************/
function sayDate(e) {
// var e = e || window.event;
var srcEle = (e)?e.target : window.event.srcElement;
var dateISO = selectedYear + '-'
+ addZ(selectedMonth+1) + '-'
+ addZ(srcEle.firstChild.data);
alert(dateISO);
}

/*************************
Adds a leading zero to single digit numbers
Only used by sayDate function
*************************/
function addZ(x){
return (x<10)?'0'+x:x;
}

/*************************
Displays or hides the calendar
*************************/
function showHideCal(c){
var ele = document.getElementById(c);
if (ele.style)
ele.style.display = (ele.style.display == '')?'none':'';
}

/*************************
Displays the next month - pass the
value to change months (0-11) by:
1 to get next month
-1 to get last month
*************************/
function changeMonth(n){
var x = +selectedMonth+n;
if ( x > 11 ) {
x -= 12;
selectedYear -= -1;
}
if ( x < 0 ) {
x -= -12;
selectedYear -= 1;
}
initCal(selectedYear,x);
}

/*************************
Displays the next year
*************************/
function changeYear(n){
initCal(+selectedYear+n,selectedMonth);
}

</script>

</head>
<body onload="
// If initialising from a server, call using current
// year and month (0-11) e.g. initCal(2005,1);
resetForm();initCal();
">
<button onclick="
showHideCal('mainDiv');
if (this.blur) this.blur();
">Show/hide calendar</button>
<button onclick="resetForm();">Reset
(back to this month)</button>
<br>
<button onclick="changeMonth(-1);">Previous Month</button>
<button onclick="changeMonth(1);">Next Month</button>
<button onclick="changeYear(-1);">Previous Year</button>
<button onclick="changeYear(1);">Next Year</button>

<div class="mainDiv" id="mainDiv" style="display: ;">
<div class="dateLabels">
<div class="monthLabel" id="monthLabel"></div>
<br>
<form name="dateSel" action="">
<select name="monthSel" onchange="
initCal(
this.form.yearSel[this.form.yearSel.selectedIndex].text,
this.selectedIndex
);
">
<!-- month options added by script -->
</select>
<select name="yearSel" onchange="
initCal(
this[this.selectedIndex].text,
this.form.monthSel.selectedIndex
);
">
<!-- year options added by script -->
</select><br>
<a href="#" onclick="
showHideCal('mainDiv'); return false;
">Close</a>
</form>
<div class="dateLabel" style="left:0em">M</div>
<div class="dateLabel" style="left:2em">T</div>
<div class="dateLabel" style="left:4em">W</div>
<div class="dateLabel" style="left:6em">T</div>
<div class="dateLabel" style="left:8em">F</div>
<div class="dateLabel" style="left:10em">S</div>
<div class="dateLabel" style="left:12em">S</div>
<div class="dateHolder" id="dateBlock"></div>
</div>
</body>
</html>
Jul 23 '05 #11

P: n/a
On Fri, 18 Feb 2005 RobG <rg***@iinet.net.auau> wrote:
Matt Kruse wrote:
Stimp wrote:

Crikey Matt, is that really "The Simplest Possible
Implementation Of A Calendar Popup" ?

For the record, here's one that, to me, is considerably simpler
- but I make no claims regarding "simplest possible"!!

Oh, it doesn't use a popup...


<SNIP!>

Thanks I'll try that one if Matt's doesn't suit :)

Jul 23 '05 #12

P: n/a
On Thu, 17 Feb 2005 Matt Kruse <ne********@mattkruse.com> wrote:
Stimp wrote:
preferably one that when clicked can update three date dropdowns (day,
month, year) like
http://www.visitdublin.com/carhire/avis.asp


You can check out mine, which has an example of updating 3 fields just like
at your URL:
http://www.JavascriptToolbox.com/calendarpopup/


That actually looks perfect... Thanks!!

--

"I hear ma train a comin'
.... hear freedom comin"
Jul 23 '05 #13

P: n/a
JRS: In article <%3*****************@news.optus.net.au>, dated Fri, 18
Feb 2005 06:38:51, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :
function getMonthDays(Y,M){
if ( M==4 || M==6 || M==9 || M==10){
return 30;
} else if ( M==2 && Y%4==0){
if ( Y%100!=0 || Y%400==0){
return 29;
}
} else if ( M==2 ) {
return 28;
}
return 31;
}


So October has 30 days in .auau ?

After if (...) return there is no need for an else.

No need to test M==2 more than once.

function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) return 30
if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
return 31 }

function getMonthDays(Y, M) {
return M==4 || M==6 || M==9 || M==11 ? 30 :
M==2 ? 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0)) : 31 }

function getMonthDays(Y, M) {
return M==4 || M==6 || M==9 || M==11 ? 30 :
M==2 ? Y%4!=0 ? 28 : Y%100!=0 ? 29 : Y%400!=0 ? 28 : 29 : 31 }

function getMonthDays(Y,M){
return M==4 || M==6 || M==9 || M==11 ? 30 :
M!=2 ? 31 : Y%4!=0 ? 28 : Y%100!=0 ? 29 : Y%400!=0 ? 28 : 29 }
The first two make a redundant test for non-centurial leap years. The
rest would be efficient if compiled, but maybe not so when interpreted.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #14

P: n/a
RobG wrote:
Crikey Matt, is that really "The Simplest Possible
Implementation Of A Calendar Popup" ?


No, you misunderstood. Perhaps I'll change the wording. The link is to an
example page which is the simplest possible implementation of my calendar
popup. It's easier for some beginners to work from a very simple example.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #15

P: n/a
Dr John Stockton wrote:
[...]

So October has 30 days in .auau ?
I think it's pronounced "Ow Ow". Thanks.

After if (...) return there is no need for an else.

No need to test M==2 more than once.

function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) return 30
if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
return 31 }
I think this one is easiest to understand, but...

function getMonthDays(Y, M) {
return M==4 || M==6 || M==9 || M==11 ? 30 :
M==2 ? 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0)) : 31 }
this one has the best trade-off between simplicity and clarity.

[...] The first two make a redundant test for non-centurial leap years. The
rest would be efficient if compiled, but maybe not so when interpreted.


These may have their good points in terms of exploitation of
JavaScript features, but testing showed them all to take almost
exactly the same amount of time.

I had to calculate the number of days in each month of they year
for 20,000 years to get reasonable numbers (about 400ms), so I
don't think optimisation for speed is required here.

I tried optimising by first testing for months with 31 days:

return M==1 || ... M==12? 30:

but it was 10% slower - I guess all those ORs are slow.

--
Rob
Jul 23 '05 #16

P: n/a
Dr John Stockton wrote:
[various getMonthDays functions]
I suggest putting braces and semi-colons in there. It makes code much more
readable and avoids mistakes by others if they want to modify the code. It's
always a good idea to use them, IMO.
function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) return 30
if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
return 31 }


I recommend this instead...

function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) {
return 30;
}
if ( M==2 ) {
if (Y%4==0 && ( Y%100!=0 || Y%400==0)) {
return 29;
}
return 28;
}
return 31;
}

It's a few bytes more than yours, but will execute with identical speed over
thousands of iterations and is much, much clearer.

Trading code clarity for compactness is a common mistake. Avoid it.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #17

P: n/a
JRS: In article <0D*****************@news.optus.net.au>, dated Mon, 21
Feb 2005 00:07:24, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :

I tried optimising by first testing for months with 31 days:

return M==1 || ... M==12? 30:

but it was 10% slower - I guess all those ORs are slow.

Just count the number of tests done for each of the 12 months, and add.

For the 7 31-day months it will be 1+2+3+4+5+6+7 = 28, for the remaining
5 months (omitting the leap part) it will be 8 each = 40, sum 68.

Test the 4 30-day months first, 1+2+3+4 = 10, the remaining 8 months
have 5 tests = 40, sum = 50.

OR should be fast; but it is another operation to do. The time taken
for operations that map well onto the instruction set will be dominated
by the time taken in handling the operands, whether compiled or
interpreted.

That's one reason why a Zeller-type arithmetic operation, if likely to
be possible, is often worth investigating - though in this case it can
be beaten.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #18

P: n/a
JRS: In article <cv********@news1.newsguy.com>, dated Sun, 20 Feb 2005
20:05:33, seen in news:comp.lang.javascript, Matt Kruse
<ne********@mattkruse.com> posted :
Dr John Stockton wrote:
[various getMonthDays functions]


I suggest putting braces and semi-colons in there. It makes code much more
readable and avoids mistakes by others if they want to modify the code. It's
always a good idea to use them, IMO.
function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) return 30
if ( M==2 ) return 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0))
return 31 }


I recommend this instead...

function getMonthDays(Y, M) {
if ( M==4 || M==6 || M==9 || M==11) {
return 30;
}
if ( M==2 ) {
if (Y%4==0 && ( Y%100!=0 || Y%400==0)) {
return 29;
}
return 28;
}
return 31;
}

It's a few bytes more than yours, but will execute with identical speed over
thousands of iterations and is much, much clearer.

Trading code clarity for compactness is a common mistake. Avoid it.


Bloated source. The more {} one has, the longer it takes a reader to
see how they match. Code is most readable if, when indented for
structure, the indentation level changes the least and there are fewest
lines.
Yours would be improved as, in part,

if ( M!=2 ) return 31 // also get that case lexically out of the way
if (Y%4==0 && ( Y%100!=0 || Y%400==0)) return 29
return 28

which has less nesting. I'd prefer to reverse the logic of the last two
lines of that, disposing of the common case first; but that should not
affect speed.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
some Astro stuff via astro.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #19

P: n/a
Dr John Stockton wrote:
Trading code clarity for compactness is a common mistake. Avoid it. Bloated source. The more {} one has, the longer it takes a reader to
see how they match.


"Bloated"? Tell me, what advantage does your version have, other than saving
space?
As a learning/instructional tool, the version I posted is far superior. It's
also a better style for making future changes to code (although this method
itself doesn't lend itself to future changes).

Code should be written clearly and explicitly in order to increase clarity
maintainability. For use in applications or sites, it is a good idea to then
compress the original source by removing unnecessary whitespace, etc. But
for development, debugging, and sharing, more readable code is better.
Code is most readable if, when indented for
structure, the indentation level changes the least and there are
fewest lines.
I don't believe this is true at all.
I think your statements go against most general rules about code
readability.
Yours would be improved as, in part,
if ( M!=2 ) return 31 // also get that case lexically out of the way
if (Y%4==0 && ( Y%100!=0 || Y%400==0)) return 29
return 28
which has less nesting.


It has less nesting, but is harder to read. Furthermore, leaving out {}
increases the risk of future bugs.
And it runs at about the same speed as the better-written version.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.