DEV Community

Discussion on: Daily Challenge #13 - Twice Linear

Collapse
 
kerrishotts profile image
Kerri Shotts • Edited

A little late to the party (3am my time), but oh well! Here's my submission:

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];
}

Full code w/ some tests: gist.github.com/kerrishotts/029c8f...

This is one where it would have been super helpful to have some answers to compare against. Beyond the initial few digits, I'm just assuming things are correct, which may not be true.