Skip to content

Commit

Permalink
Merge pull request #10 from ilbertt/fix-merch-prizes-randomness
Browse files Browse the repository at this point in the history
fix merch prizes randomness
  • Loading branch information
ilbertt authored May 24, 2024
2 parents 622a21c + ab75b6f commit de6d630
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/fortune-wheel-booth-frontend/src/utils/findPrize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { PRIZES } from '../costants';

const getMerchPrizesIndexes = () => {
const merchPrizes: number[] = PRIZES.reduce((acc: number[], prize, index) => {
if (prize?.option?.includes('merch')) {
acc.push(index);
}
return acc;
}, []);

return merchPrizes;
};

export const findPrize = (option: string) => {
const prize = PRIZES.find((prize, index) => prize.option === option);
return prize ? PRIZES.indexOf(prize) : 99; // TODO: Fix this, 99 represents "no prize exists for this option"
if (option === 'merch') {
const merchPrizesIndexes: number[] = getMerchPrizesIndexes();
return merchPrizesIndexes[
Math.floor(Math.random() * merchPrizesIndexes.length)
];
}
return PRIZES.findIndex((prize) => prize.option === option); // Returns the index of the first element in the array where predicate is true, and -1 otherwise.
};

0 comments on commit de6d630

Please sign in to comment.