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.180 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.179 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.178 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.177 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.176 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.175 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.174 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.173 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.172 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.171 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.170 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.169 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.167 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.166 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.165 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.164 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.163 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.162 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.161 tracks/cfml/exercises/atbash-cipher/Solution.cfc
trackler-2.2.1.160 tracks/cfml/exercises/atbash-cipher/Solution.cfc