Created
July 20, 2019 04:48
-
-
Save kerrishotts/85214dd3fd0131283dc137d694930566 to your computer and use it in GitHub Desktop.
2019-07-18 Dev.to challenge
This file contains 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
const makeRun = (v, {ofLength = 1} = {}) => Array.from({length: ofLength}, () => v).join(""); | |
const hasTripleTrouble = (triple, double) => { | |
const [tripleStr, doubleStr] = [triple, double].map(v => String(v)); | |
return (Array.from({length: 10}, (_, idx) => idx) | |
.some(digit => (tripleStr.indexOf(makeRun(digit, {ofLength: 3})) > -1 | |
&& doubleStr.indexOf(makeRun(digit, {ofLength: 2})) > -1))); | |
} | |
const testCases = [ | |
{ args: [123, 12345], result: false }, | |
{ args: [1222245551, 1554223], result: true }, | |
{ args: [451999277, 41177722899], result: true }, | |
{ args: [1222345, 12345], result: false }, | |
{ args: [12345, 12345], result: false }, | |
{ args: [666789, 12345667], result: true }, | |
]; | |
const testAllCases = (fn, cases) => | |
cases.map(({args, result}) => [`${args} -> ${result}`, fn(...args) === result]) | |
.filter(([_, pass]) => !pass); | |
const failures = testAllCases(hasTripleTrouble, testCases); | |
if (failures.length > 0) { | |
throw new Error(JSON.stringify(failures), 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment