You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

4 years ago
  1. 'use strict';
  2. const SelectPrompt = require('./select');
  3. class Quiz extends SelectPrompt {
  4. constructor(options) {
  5. super(options);
  6. if (typeof this.options.correctChoice !== 'number' || this.options.correctChoice < 0) {
  7. throw new Error('Please specify the index of the correct answer from the list of choices');
  8. }
  9. }
  10. async toChoices(value, parent) {
  11. let choices = await super.toChoices(value, parent);
  12. if (choices.length < 2) {
  13. throw new Error('Please give at least two choices to the user');
  14. }
  15. if (this.options.correctChoice > choices.length) {
  16. throw new Error('Please specify the index of the correct answer from the list of choices');
  17. }
  18. return choices;
  19. }
  20. check(state) {
  21. return state.index === this.options.correctChoice;
  22. }
  23. async result(selected) {
  24. return {
  25. selectedAnswer: selected,
  26. correctAnswer: this.options.choices[this.options.correctChoice].value,
  27. correct: await this.check(this.state)
  28. };
  29. }
  30. }
  31. module.exports = Quiz;