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

kdkaergaard's solution

to Raindrops in the CFML Track

Published at Mar 07 2021 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

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

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

RaindropsTest.cfc

component extends="testbox.system.BaseSpec" {

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

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

			it( 'the sound for 1 is 1', function(){
				expect( SUT.convert( number='1' ) ).toBe( '1' );
			});

			it( 'the sound for 3 is Pling', function(){
				expect( SUT.convert( number='3' ) ).toBe( 'Pling' );
			});

			it( 'the sound for 5 is Plang', function(){
				expect( SUT.convert( number='5' ) ).toBe( 'Plang' );
			});

			it( 'the sound for 7 is Plong', function(){
				expect( SUT.convert( number='7' ) ).toBe( 'Plong' );
			});

			it( 'the sound for 6 is Pling as it has a factor 3', function(){
				expect( SUT.convert( number='6' ) ).toBe( 'Pling' );
			});

			it( '2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', function(){
				expect( SUT.convert( number='8' ) ).toBe( '8' );
			});

			it( 'the sound for 9 is Pling as it has a factor 3', function(){
				expect( SUT.convert( number='9' ) ).toBe( 'Pling' );
			});

			it( 'the sound for 10 is Plang as it has a factor 5', function(){
				expect( SUT.convert( number='10' ) ).toBe( 'Plang' );
			});

			it( 'the sound for 14 is Plong as it has a factor of 7', function(){
				expect( SUT.convert( number='14' ) ).toBe( 'Plong' );
			});

			it( 'the sound for 15 is PlingPlang as it has factors 3 and 5', function(){
				expect( SUT.convert( number='15' ) ).toBe( 'PlingPlang' );
			});

			it( 'the sound for 21 is PlingPlong as it has factors 3 and 7', function(){
				expect( SUT.convert( number='21' ) ).toBe( 'PlingPlong' );
			});

			it( 'the sound for 25 is Plang as it has a factor 5', function(){
				expect( SUT.convert( number='25' ) ).toBe( 'Plang' );
			});

			it( 'the sound for 27 is Pling as it has a factor 3', function(){
				expect( SUT.convert( number='27' ) ).toBe( 'Pling' );
			});

			it( 'the sound for 35 is PlangPlong as it has factors 5 and 7', function(){
				expect( SUT.convert( number='35' ) ).toBe( 'PlangPlong' );
			});

			it( 'the sound for 49 is Plong as it has a factor 7', function(){
				expect( SUT.convert( number='49' ) ).toBe( 'Plong' );
			});

			it( 'the sound for 52 is 52', function(){
				expect( SUT.convert( number='52' ) ).toBe( '52' );
			});

			it( 'the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', function(){
				expect( SUT.convert( number='105' ) ).toBe( 'PlingPlangPlong' );
			});

			it( 'the sound for 3125 is Plang as it has a factor 5', function(){
				expect( SUT.convert( number='3125' ) ).toBe( 'Plang' );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="RaindropsTest" {

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

}
/**
* Your implementation of the Raindrops exercise
*/
component {

	/**
	* store test information only once
	*/
	static {
		static.divisors = [
			{ value = 3, text = 'Pling' },
			{ value = 5, text = 'Plang' },
			{ value = 7, text = 'Plong' }
		]
	}
	
	/**
	* @returns 
	*/
	function convert( number ) {
		returnString = ''
		static.divisors.each(function( divisor ) {
			if ( number % divisor.value == 0 ) {
				returnString = returnString & divisor.text
			}
		})
		if ( isEmpty(returnString) ) {
			return number
		}
		return returnString
	}
	
}

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?