<s>[INST] function trace_upper(f, y) {
    var sides = [];
    for (var i = 0; i < y; i++) {
        for (var n = i; n < y; n++) {
            sides.push(Math.round(f * n));
        }
    }
    sides.sort(function (a, b) {
        return a - b;
    });
    sides = [...new Set(sides)];
    sides.unshift(0);
    return sides;
}

function trace_lower(f, y) {
    var sides = [];
    for (var i = 1; i < y; i++) {
        for (var n = i; n < y; n++) {
            sides.push(Math.round(f * n));
        }
    }
    sides.sort(function (a, b) {
        return a - b;
    });
    sides = [...new Set(sides)];
    sides.unshift(0);
    return sides;
}

function range_upper(x, y, f) {
    var origin = trace_upper(f, y), len = origin.length - 1,
        result = [];
    for (var i = 0; i < x; i++) {
        result.push(origin[len]);
    }
    return result;
}

function range_lower(x, y, f) {
    var origin = trace_lower(f, y), len = origin.length - 1,
        result = [];
    for (var i = 0; i < x; i++) {
        result.push(origin[len]);
    }
    return result;
}
This code is quite repetitive. How can I merge the functions trace_upper and trace_lower into one function?[/INST]