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.