Last active
December 22, 2015 06:58
-
-
Save apaatsio/6434694 to your computer and use it in GitHub Desktop.
Solution for Reaktor's programming task at http://reaktor.fi/ura/fast_track/ This solution relies on the fact that JavaScript can solve multiplication for strings (e.g. "2" * "3" -> 6) and thus parseInt is not needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function (input) { | |
function product(numbers) { | |
if (numbers.indexOf("0") !== -1) { | |
return 0 | |
} else { | |
return _(numbers).reduce(function(product, number){ | |
return product * number | |
}, 1) | |
} | |
} | |
function getNumberGroup(firstNumber, index, allNumbers) { | |
return allNumbers.substr(index, 5) | |
} | |
function hasFiveNumbers(numbers) { | |
return numbers.length === 5 | |
} | |
var biggestProduct =_(input).chain() | |
.map(getNumberGroup) | |
.filter(hasFiveNumbers) | |
.map(product) | |
.max() | |
.value() | |
return biggestProduct | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment