Last active
August 16, 2018 08:09
-
-
Save bmelnychuk/ba6dec50fd1db7788bdd295a039e2ba8 to your computer and use it in GitHub Desktop.
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
class RandomNextQuestionPolicy implements NextQuestionPolicy { | |
async getNextQuestion(availableQuestions: Question[]): Promise<Question> { | |
if (availableQuestions.length === 0) | |
throw new Error('Next question is not available'); | |
const nextRandomIndex = RandomNextQuestionPolicy.getRandomInt( | |
0, availableQuestions.length - 1 | |
); | |
return Promise.resolve(availableQuestions[nextRandomIndex]); | |
} | |
/** | |
* Returns a random number between min and max [min, max] | |
*/ | |
static getRandomInt(min: number, max: number): number { | |
return Math.floor(Math.random() * ((max - min) + 1)) + min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment