Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.