Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Created July 12, 2019 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerrishotts/029c8f1ab27daf3833a9787834e8b756 to your computer and use it in GitHub Desktop.
Save kerrishotts/029c8f1ab27daf3833a9787834e8b756 to your computer and use it in GitHub Desktop.
2019-07-11 Dev.to Challenge
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