|
| 1 | +(function () { |
| 2 | + var Question = function (question, answers, correct) { |
| 3 | + this.question = question; |
| 4 | + this.answers = answers; |
| 5 | + this.correct = correct; |
| 6 | + } |
| 7 | + |
| 8 | + Question.prototype.display = function () { |
| 9 | + console.log(this.question); |
| 10 | + for (var i = 0; i < this.answers.length; i++) |
| 11 | + console.log((i + 1) + '. ' + this.answers[i]); |
| 12 | + } |
| 13 | + |
| 14 | + Question.prototype.checkAnswer = function (answer) { |
| 15 | + if (answer === this.correct) |
| 16 | + console.log('Correct!'); |
| 17 | + else |
| 18 | + console.log('incorrect. Try again! :)'); |
| 19 | + } |
| 20 | + |
| 21 | + var question_1 = new Question('Is JavaScript the coolest programming language in the world?', ['Yes', 'No'], 1); |
| 22 | + var question_2 = new Question('How many days are 1 week?', ['5days', '9days', '7days', '11days'], 3); |
| 23 | + var arr = [question_1, question_2]; |
| 24 | + var num = Math.floor(Math.random() * arr.length); |
| 25 | + arr[num].display(); |
| 26 | + var answer = parseInt(prompt('Enter the number of the correct answer')); |
| 27 | + arr[num].checkAnswer(answer); |
| 28 | + |
| 29 | +})(); |
0 commit comments