From 293485eb4e3e00bf76517fd51124afc18d19a6ee Mon Sep 17 00:00:00 2001 From: Tailszefox Date: Sun, 1 Sep 2024 19:22:13 +0200 Subject: [PATCH] Randomizing now checks that the randomized index is different from the current one --- js/base.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/base.js b/js/base.js index 436932c..cfddc1e 100644 --- a/js/base.js +++ b/js/base.js @@ -66,10 +66,18 @@ $(function () var nbPonies = $("#from option").size(); + function getRandomIndex(currentIndex, maxIndex) { + var newIndex; + do { + newIndex = Math.floor(Math.random() * maxIndex); + } while (newIndex === currentIndex); + return newIndex; + } + if(which == 0) { - $("#from")[0].selectedIndex = Math.floor(Math.random() * nbPonies); - $("#to")[0].selectedIndex = Math.floor(Math.random() * nbPonies); + $("#from")[0].selectedIndex = getRandomIndex($("#from")[0].selectedIndex, nbPonies); + $("#to")[0].selectedIndex = getRandomIndex($("#to")[0].selectedIndex, nbPonies); if($("#from")[0].selectedIndex == $("#to")[0].selectedIndex) { @@ -81,7 +89,7 @@ $(function () } else if(which == 1) { - $("#from")[0].selectedIndex = Math.floor(Math.random() * nbPonies); + $("#from")[0].selectedIndex = getRandomIndex($("#from")[0].selectedIndex, nbPonies); if($("#from")[0].selectedIndex == $("#to")[0].selectedIndex) { @@ -93,7 +101,7 @@ $(function () } else if(which == 2) { - $("#to")[0].selectedIndex = Math.floor(Math.random() * nbPonies); + $("#to")[0].selectedIndex = getRandomIndex($("#to")[0].selectedIndex, nbPonies); if($("#from")[0].selectedIndex == $("#to")[0].selectedIndex) {