-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ilbertt/fix-merch-prizes-randomness
fix merch prizes randomness
- Loading branch information
Showing
1 changed file
with
18 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}; |