🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of Mr-Lizard

Mr-Lizard's solution

to Leap in the CFML Track

Published at Sep 15 2020 · 0 comments
Instructions
Test suite
Solution

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.


To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

LeapTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'Leap' );
	}

	function run(){
	
		describe( "My Leap class", function(){			

			it( 'year not divisible by 4: common year', function(){
				expect( SUT.leapYear( year='2015' ) ).toBeFalse();
			});

			it( 'year divisible by 4, not divisible by 100: leap year', function(){
				expect( SUT.leapYear( year='1996' ) ).toBeTrue();
			});

			it( 'year divisible by 100, not divisible by 400: common year', function(){
				expect( SUT.leapYear( year='2100' ) ).toBeFalse();
			});

			it( 'year divisible by 400: leap year', function(){
				expect( SUT.leapYear( year='2000' ) ).toBeTrue();
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="LeapTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}
component {

	/**
	* @year The input year to consider
	*
	* @returns A boolean for whether the inputted year is true or false

	on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

	*/
	 function leapYear( year ) {
			 if ((year % 100 == 0) && (year % 400 > 0))
			    return false;	 

		return (year % 4 == 0);
	}
	
}

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?