Sha256: a1190ee7f2649c698bec1c65789c3635e6a65be30a74f2dc261e07794ec24345

Contents?: true

Size: 884 Bytes

Versions: 146

Compression:

Stored size: 884 Bytes

Contents

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

	function largestProduct( digits, span ) {
		// Empty span is 1
		if( span == 0 ) {
			return 1;
		}
		// reject negative span
		if( span < 0 ) {
			return -1;
		}
		// reject span longer than digits
		if( span > digits.len() ) {
			return -1;
		}
		// Regect non-numeric characters in digits
		if( digits.reFind( '[^0-9]' ) ) {
			return -1;
		}
		
		var offset = 0;
		var largestProduct = 0;
		// Loop through digits
		while( ++offset <= digits.len()-span+1 ) {
			// Take the larger of the previous largest product...
			largestProduct = max( largestProduct,
				// or the product of the current spanned digits
				digits.mid( offset, span )
					.listToArray( '' )
					.reduce( function( prev, digit ){
						return prev * val( digit );
					}, 1 ) ); 
		}
		
		return largestProduct;
	}
	
}

Version data entries

146 entries across 145 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.179 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.178 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.177 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.176 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.175 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.174 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.173 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.172 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.171 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.170 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.169 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.167 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.166 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.165 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.164 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.163 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.162 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.161 tracks/cfml/exercises/largest-series-product/Solution.cfc
trackler-2.2.1.160 tracks/cfml/exercises/largest-series-product/Solution.cfc