Sha256: 6f7a82065f6a34b165c479efec87bb8498f630b0da3952902ec2b522122d6b29

Contents?: true

Size: 1.18 KB

Versions: 146

Compression:

Stored size: 1.18 KB

Contents

/**
* Here is an example solution for the AtbashCipher exercise
*/
component {
	
	function encode( phrase ) {
		var result = '';
		var breakCounter = 0;
		
		phrase
			.lcase()
			// Clean anything not alphanumeric
			.reReplace( '[^0-9a-z]', '', 'all' )
			.listToArray( '' )
			// Loop over each char
			.each( function( char ) {
				breakCounter++;
				result &= invertChar( char );				
				// Every 5th char gets a space
				if( breakCounter % 5 == 0 ) {
					result &= ' ';
				}
			} );
			
		// Nuke trailing spaces
		return result.trim();
	}
	
	function decode( phrase ) {
		var result = '';
		
		phrase
			.lcase()
			// Clean anything not alphanumeric
			.reReplace( '[^0-9a-z]', '', 'all' )
			.listToArray( '' )
			// Loop over each char
			.each( function( char ) {
				result &= invertChar( char );
			} );
			
		return result;
	}
	
	private function invertChar( char ) {		
		// Pass numbers straight through
		if( asc( char ) >= 49 && asc( char ) <= 57 ) {
			return char;
		} else {
			// For letters, find the offset from "a" and reverse it from "z"
			// Using the ascii codes as a handy reference intead of creating an array of chars
			return chr( 122 - ( asc( char ) - 97 )  );
		}
	}
	
}

Version data entries

146 entries across 145 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.138 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.137 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.136 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.135 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.134 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.133 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.132 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.131 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.130 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.129 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.128 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.127 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.126 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.125 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.124 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.123 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.122 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.121 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.120 tracks/cfml/exercises/atbash-cipher/Solution.cfc