Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#result { font-size: 2rem; font-weight: bold; color: #007bff; margin: 20px 0; min-height: 40px; }
button { padding: 10px 20px; font-size: 1rem; border: none; background: #007bff; color: white; border-radius: 5px; cursor: pointer; }
button:hover { background: #0056b3; }
button.secondary { background: #6c757d; }
button.secondary:hover { background: #545b62; }
#error { color: red; font-style: italic; }
</style>
</head>
Expand All @@ -19,6 +21,7 @@
<h1>Random Picker</h1>
<div id="result">...</div>
<button onclick="pickRandom()">Pick Again</button>
<button onclick="resetPool()" class="secondary">Reset</button>
<p id="error"></p>
</div>

Expand All @@ -31,19 +34,54 @@ <h1>Random Picker</h1>
items = itemsParam.split(',').map(i => i.trim()).filter(i => i);
}

// Pool of not-yet-picked items, persisted so reloads don't repeat entries.
const storageKey = 'randomPicker:' + [...items].sort().join(',');

function loadState() {
try {
const state = JSON.parse(localStorage.getItem(storageKey));
if (state && Array.isArray(state.remaining)) return state;
} catch (e) {}
return { remaining: [], lastPick: null };
}

function resetPool() {
localStorage.removeItem(storageKey);
document.getElementById('result').innerText = "...";
document.getElementById('error').innerText = "";
}

function pickRandom() {
const resultDiv = document.getElementById('result');
const errorDiv = document.getElementById('error');

if (items.length === 0) {
resultDiv.innerText = "...";
errorDiv.innerText = "Error: Add items to the URL. Example: ?items=Apple,Banana,Orange";
return;
}

errorDiv.innerText = "";
const randomIndex = Math.floor(Math.random() * items.length);
resultDiv.innerText = items[randomIndex];
const state = loadState();

const isRefill = state.remaining.length === 0;
if (isRefill) {
state.remaining = [...items];
}

// Right after a refill, don't repeat the entry the last round ended on.
let candidates = state.remaining;
if (isRefill && items.length > 1) {
const withoutLast = state.remaining.filter(i => i !== state.lastPick);
if (withoutLast.length > 0) candidates = withoutLast;
}

const pick = candidates[Math.floor(Math.random() * candidates.length)];
state.remaining.splice(state.remaining.indexOf(pick), 1);

state.lastPick = pick;
localStorage.setItem(storageKey, JSON.stringify(state));
resultDiv.innerText = pick + (state.remaining.length ? '' : ' 🔄');
}

// Run automatically on page load
Expand Down