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.104 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.103 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.102 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.101 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.100 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.99 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.98 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.97 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.96 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.95 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.94 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.93 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.92 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.91 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.90 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.89 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.88 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.87 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.86 tracks/cfml/exercises/luhn/Solution.cfc
trackler-2.2.1.85 tracks/cfml/exercises/luhn/Solution.cfc