# ssinha's solution

## to Leap in the CFML Track

Published at Apr 24 2019 · 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
``````

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' );
}

}``````
``````/**
* 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 ) {
// Implement me here
var flag = false;
if(isNumeric(year)){
if(year % 4 eq 0 and (year % 100 neq 0 or year % 400 EQ 0)){
flag = true;
}
}else{
flag = false;
}
return flag;
}

}``````