Created
July 12, 2019 07:00
-
-
Save kerrishotts/029c8f1ab27daf3833a9787834e8b756 to your computer and use it in GitHub Desktop.
2019-07-11 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
function dblLinear(n) { | |
const series = [1]; | |
const calc = x => ({ | |
y: 2 * x + 1, | |
z: 3 * x + 1 | |
}); | |
const ascendingOrder = (a, b) => a - b; | |
for (let idx = 0; idx <= n; idx++) { | |
let x = series[idx]; | |
const { y, z } = calc(x); | |
for (let v of [y, z]) { | |
if (series.indexOf(v) < 0) { | |
series.push(v); | |
series.sort(ascendingOrder); | |
series.splice(n+1); | |
} | |
} | |
} | |
return series[n]; | |
} | |
const assert = expr => { | |
if (!expr) { | |
throw new Error("Assertion failed."); | |
} | |
} | |
assert(dblLinear(10) === 22); | |
assert(dblLinear(11) === 27); | |
assert(dblLinear(12) === 28); | |
assert(dblLinear(90) === 379); | |
assert(dblLinear(100) === 447); | |
assert(dblLinear(1000) === 8488); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment