Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of loveJesus

loveJesus's solution

to Leap in the CFML Track

Published at Jun 04 2021 · 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' );
	}

}
/**
 * For God so loved the world, that He gave His only begotten Son, 
 * that all who believe in Him should not perish but have everlasting life.
 * Your implmentation of the Leap exercise
 */
component {
    
    /**
    * @year The input year to consider
    *
    * @returns A boolean for whether the inputted year is true or false
    */
     function leapYear( year ) {
        return  year % 4 == 0 
                && year % 100 != 0 
                || year % 400 == 0;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

loveJesus's Reflection

Hallelujah!