Sha256: 138f897ff9388813bf722a0b7153193ece42d9100c3d5b430694eb3938968bdb

Contents?: true

Size: 827 Bytes

Versions: 72

Compression:

Stored size: 827 Bytes

Contents

/**
* Here is an example solution for the Luhn exercise
*/
component {

	function valid( input ) {
		// Remove whitespace
		input = input.reReplace( '\s', '', 'all' );
		
		// Any non numeric chars means invalid
		if( input.reFindNoCase( '[^0-9]' ) ) {
			return false;
		}
		
		// A single 0 is invalid
		if( compare( input, '0' ) == 0 ) {
			return false;
		}
		
		var accumulator = [];
		var oddEven = 0;
		// Loop backwards over the digits
		for( var i=input.len(); i>0; i-- ) {
			var digit = input[ i ];
			// Double every other one
			if( ++oddEven%2 == 0 ) {
				var doubleDigit = digit*2;
				accumulator.prepend( ( doubleDigit > 9 ? doubleDigit-9 : doubleDigit ) );
			} else {
				accumulator.prepend( digit );
			}
		}
		
		// Is the sum of the digits divisible by 10?
		return accumulator.sum() % 10 == 0;
	}
			
}

Version data entries

72 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.64 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.63 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.62 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.61 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.60 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.59 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.58 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.57 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.56 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.55 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.54 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.53 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.52 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.51 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.50 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.49 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.48 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.47 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.46 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.45 tracks/cfml/exercises/luhn/Solution.cfc