|
| 1 | + |
| 2 | +// ----------------------------------------------------------------------------- |
| 3 | +// Coding Challenge 4 |
| 4 | +// ----------------------------------------------------------------------------- |
| 5 | + |
| 6 | +/* |
| 7 | +--- Let's build a fun quiz game in the console! --- |
| 8 | + |
| 9 | +1. Build a function constructor called Question to describe a question. |
| 10 | + A question should include: |
| 11 | + a) The question itself |
| 12 | + b) The answers from which the player can choose the correct one |
| 13 | + (choose an adequate data structure here, array, object, etc.) |
| 14 | + c) The correct answer (I would use a number for this) |
| 15 | + |
| 16 | +2. Create a couple of questions using the constructor |
| 17 | + |
| 18 | +3. Store them all inside an array |
| 19 | + |
| 20 | +4. Select one random question and log it on the console, together with the possible |
| 21 | + answers (each question should have a number) (Hint: write a method for the |
| 22 | + Question objects for this task). |
| 23 | + |
| 24 | +5. Use the 'prompt' function to ask the user for the correct answer. The user should |
| 25 | + input the number of the correct answer such as you displayed it on Task 4. |
| 26 | + |
| 27 | +6. Check if the answer is correct and print to the console whether the answer is |
| 28 | + correct or not (Hint: write another method for this). |
| 29 | + |
| 30 | +7. Suppose this code would be a plugin for other programmers to use in their code. |
| 31 | + So make sure that all your code is private and doesn't interfere with the other |
| 32 | + programmers code (Hint: we learned a special technique to do exactly that). |
| 33 | + |
| 34 | +8. After you display the result, display the next random question, so that the game never |
| 35 | + ends (Hint: write a function for this and call it right after displaying the result) |
| 36 | + |
| 37 | +9. Be careful: after Task 8, the game literally never ends. So include the option to |
| 38 | + quit the game if the user writes 'exit' instead of the answer. In this case, |
| 39 | + DON'T call the function from task 8. |
| 40 | + |
| 41 | +10. Track the user's score to make the game more fun! So each time an answer is correct, |
| 42 | + add 1 point to the score (Hint: I'm going to use the power of closures for this, |
| 43 | + but you don't have to, just do this with the tools you feel more comfortable at this |
| 44 | + point). |
| 45 | + |
| 46 | +11. Display the score in the console. Use yet another method for this. |
| 47 | +*/ |
| 48 | + |
| 49 | +// Constructor Function |
| 50 | +// This will be called to create new instances of a Question. |
| 51 | +var Question = function(question, choices, answer) { |
| 52 | + this.question = question; |
| 53 | + this.choices = choices; |
| 54 | + this.answer = answer; |
| 55 | +}; |
| 56 | +// Prototype for Question |
| 57 | +// This will be inherited by all Question objects |
| 58 | +Question.prototype.printQuestion = function() { |
| 59 | + console.log(this.question); |
| 60 | + for (var idx = 0; idx < this.choices.length; idx++) { |
| 61 | + console.log(`${idx}: ${this.choices[idx]}`); |
| 62 | + } |
| 63 | +}; |
| 64 | +Question.prototype.compareWithAnswer = function(ans) { |
| 65 | + var isCorrect = (this.answer === parseInt(ans)); |
| 66 | + console.log(isCorrect ? "You are correct!" : "You are wrong!"); |
| 67 | + return isCorrect; |
| 68 | +} |
| 69 | + |
| 70 | +var q1 = new Question( |
| 71 | + "What is 1+1?", |
| 72 | + ["2", "5", "3", "0"], |
| 73 | + 0 |
| 74 | +); |
| 75 | +var q2 = new Question( |
| 76 | + "What is the capital of Poland?", |
| 77 | + ["Lisbon", "Warsaw", "Helsinki", "Taipei", "Amsterdam"], |
| 78 | + 1 |
| 79 | +) |
| 80 | +var q3 = new Question( |
| 81 | + "What is the currency in Japan?", |
| 82 | + ["Dollar", "Peso", "Yen"], |
| 83 | + 2 |
| 84 | +) |
| 85 | + |
| 86 | +var questions = [q1, q2, q3]; |
| 87 | +var currQ = null; |
| 88 | +var currA = null; |
| 89 | + |
| 90 | +// There is no need to use closures here, but this |
| 91 | +// exercise's lecture introduced that, let's use it. |
| 92 | +var updateScoreFunc = function () { |
| 93 | + // Instead of a global variable, the score |
| 94 | + // is wrapped inside the updateScoreFunc, |
| 95 | + // and is now maintained by the wherever |
| 96 | + // the returned function is assigned to. |
| 97 | + var score = 0; |
| 98 | + return function(isCorrect) { |
| 99 | + score += (isCorrect ? 1 : 0); |
| 100 | + console.log(`Your current score is: ${score}`); |
| 101 | + } |
| 102 | +} |
| 103 | +var updateScore = updateScoreFunc(); |
| 104 | + |
| 105 | +function promptQuestion() { |
| 106 | + var qN = Math.floor(Math.random() * questions.length); |
| 107 | + this.currQ = this.questions[qN]; |
| 108 | + this.currQ.printQuestion(); |
| 109 | + this.currA = prompt("What is the answer?") |
| 110 | + console.log(`You answered: ${this.currA}`); |
| 111 | +} |
| 112 | + |
| 113 | +function isCorrectAnswer() { |
| 114 | + return this.currQ.compareWithAnswer(this.currA); |
| 115 | +} |
| 116 | + |
| 117 | +function repeatUntilExit() { |
| 118 | + this.promptQuestion(); |
| 119 | + this.updateScore(this.isCorrectAnswer()); |
| 120 | + |
| 121 | + console.log("----------------") |
| 122 | +} |
| 123 | + |
| 124 | +while (this.currA !== "exit") { |
| 125 | + repeatUntilExit(); |
| 126 | +} |
0 commit comments