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

killv5's solution

to Space Age in the CFML Track

Published at Feb 27 2019 · 0 comments
Instructions
Test suite
Solution

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, 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

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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

SolutionTest.cfc

component extends="SpaceAgeTest" {

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

}

SpaceAgeTest.cfc

component extends="testbox.system.BaseSpec" {

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

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

			it( 'age on Earth', function(){
				expect( SUT.age( planet='Earth', seconds='1000000000' ) ).toBe( '31.69' );
			});

			it( 'age on Mercury', function(){
				expect( SUT.age( planet='Mercury', seconds='2134835688' ) ).toBe( '280.88' );
			});

			it( 'age on Venus', function(){
				expect( SUT.age( planet='Venus', seconds='189839836' ) ).toBe( '9.78' );
			});

			it( 'age on Mars', function(){
				expect( SUT.age( planet='Mars', seconds='2329871239' ) ).toBe( '39.25' );
			});

			it( 'age on Jupiter', function(){
				expect( SUT.age( planet='Jupiter', seconds='901876382' ) ).toBe( '2.41' );
			});

			it( 'age on Saturn', function(){
				expect( SUT.age( planet='Saturn', seconds='3000000000' ) ).toBe( '3.23' );
			});

			it( 'age on Uranus', function(){
				expect( SUT.age( planet='Uranus', seconds='3210123456' ) ).toBe( '1.21' );
			});

			it( 'age on Neptune', function(){
				expect( SUT.age( planet='Neptune', seconds='8210123456' ) ).toBe( '1.58' );
			});

		});
		
	}
 
}
/**
* Your implmentation of the SpaceAge exercise
*/
component {
	
	/**
	* @returns 
	*/
	 function age( planet, seconds ) {
		dict = {'Earth':1,
			'Mercury':0.2408467,
			'Venus': 0.61519726,
			'Mars': 1.8808158,
			'Jupiter': 11.862615,
			'Saturn': 29.447498,
			'Uranus': 84.016846,
			'Neptune': 164.79132};
		return numberFormat(seconds/(31557600*dict[planet]), '__.00');
	}
	
}

Community comments

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

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?