Problem Shuffling Integers in Array

What’s up, Velo gurus? I hope you’re crushing it today.

Here’s the problem:

I create and populate an array with a set of integers on page load.

On button-click, I want to shuffle the integers in the array but have no repeats.

I’ve been over this a hundred times and can’t see me error.

Here’s my code:

export function randomize_click(event) {
    randomize = "Y";
    $w("#randomRelease").enable();
    // It all happens in here, for now. Move to separate, all-serving function at second use case
    /////////////////////////////////////////////////////////////////////////////////////////////
    let count = favarray.length;
    rancount = 0;
    for (let i = rancount; i < count; i++) {
        if (i == 0) {
            randomIndex = Math.floor(Math.random() * (count));
            lastRandomIndex = randomIndex;
            ranarray[i] = lastRandomIndex;
            rancount = (i + 1);
        } else {
            randomIndex = Math.floor(Math.random() * (count));
            if (randomIndex !== lastRandomIndex) {
                ranarray[i] = randomIndex;
                lastRandomIndex = randomIndex;
            } else {
                rancount = (i - 1);
            }
        }
    }
    $w("#test").text = ranarray.toString();
    ////////////////////////////////////////////
    //tracks();
}

My results are random but still have duplicates.

Any insights would be most welcomed.

Also, is there a simpler way to do this?

I need both “favarray” and “ranarray,” so my users can “unrandomize” their favorites. The “unrandomize” (“randomizeRelease”) button is a simple boolean, not yet coded. I can do that easy enough later.

Thank you so much, everyone.

function shuffleArray(array) {
    for (let i = array.length - 1; i  > 0;  i--) {
        const j = Math.floor(Math.random() * array.length);
        [array[i], array[j]] = [array[j], array[i]];
    }
return array;
}

Thank you so much for the assist. It took a bit for me to adapt my code. (I have a couple of arrays working in parallel to the ones I’m shuffling.) But I’ve got it up and running, and it’s great.

Thanks again!