Skip to content

Commit

Permalink
Merge pull request #1 from csarnataro/shuffle-users
Browse files Browse the repository at this point in the history
Shuffling timers
  • Loading branch information
davegarthsimpson authored Jan 22, 2025
2 parents 1e84984 + dde2558 commit dfbfbb4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { ParamKeyValuePair, useSearchParams } from "react-router-dom";
import { Timer } from "./Timer";
import { timer } from "./utils";

/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(inputArray: [string, string][]): [string, string][] {
const array = [...inputArray]
for (let i = array.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array
}

export function App(): React.ReactElement {
const [timers, setTimers] = useState<timer[]>([]);
let [searchParams, setSearchParams] = useSearchParams();
Expand All @@ -13,7 +23,7 @@ export function App(): React.ReactElement {
try {
const qsTimers = [...searchParams];
setTimers(
qsTimers.map((timer) => ({ time: parseInt(timer[1]), name: timer[0] }))
shuffleArray(qsTimers).map((timer) => ({ time: parseInt(timer[1]), name: timer[0] }))
);
} catch (e) {
setTimers([]);
Expand Down

0 comments on commit dfbfbb4

Please sign in to comment.