Skip to content

Instantly share code, notes, and snippets.

@bmelnychuk
Last active August 16, 2018 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmelnychuk/ba6dec50fd1db7788bdd295a039e2ba8 to your computer and use it in GitHub Desktop.
Save bmelnychuk/ba6dec50fd1db7788bdd295a039e2ba8 to your computer and use it in GitHub Desktop.
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