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