Friday Puzzler: The Days of The Week

Here is a little puzzler that I think will be fun, and hopefully much simpler than my last one. For today’s puzzler, you must use ColdFusion to create a function that returns the day of the week (numerical) for a date. I know what you’re thinking - doesn’t ColdFusion have that built in? It does. But your task is to recreate it. I have absolutely no guidelines for how you recreate it - except that you can’t - obviously - use the same logic that someone else does. While the winner of these things are pretty much always picked arbitrarily, today’s is going to be even more crazy. I want to see the most weird, stupid, insane, etc, ways to solve this problem. Go crazy. The only restriction is that you have to run your code via a test harness to ensure it works right. I’ve written one for you. It allows you to pass in your UDF and it confirms it works for a large sample of dates.

<cffunction name=”testHarness”> <cfargument name=”myfunc” required=”true”> <cfset var dates = []> <cfset var x = ““> <cfset var result = {}>

&lt;!--- first make the dates ---&gt;
&lt;cfloop index="x" from="1" to="100"&gt;
	&lt;cfset arrayAppend(dates, dateAdd("d", randRange(-1000,1000), now()))&gt;
&lt;/cfloop&gt;	

&lt;cfloop index="x" from="1" to="#arrayLen(dates)#"&gt;
	&lt;cfif dayOfWeek(dates[x]) neq myfunc(dates[x])&gt;
		&lt;cfset result.status = "fail"&gt;
		&lt;cfset result.message = "Your function said the DOW for #dateformat(dates[x])# was #myfunc(dates[x])# and it should be #dayofweek(dates[x])#"&gt;
		&lt;cfreturn result&gt;
	&lt;/cfif&gt;
&lt;/cfloop&gt;

&lt;cfset result.status = "pass"&gt;
&lt;cfreturn result&gt; &lt;/cffunction&gt; </code>

And as an example, check this one. It will fail - eventually.

<cfscript> function mycheatfunc(d) { if(dayofweek(d) == 7) return 8; return dayofweek(d); } </cfscript>

<cfset res = testHarness(mycheatfunc)> <cfdump var=”#res#” label=”The Result”> </code>

Unfortunately, I don’t have anything to give away today - except the pride of being called King Nerd. So get cracking!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments