I have made a little game in JS. Are there any remarks? The game asks a question through the console and accepts the answer through prompt. If the answer is correct you'll get points. To stop the game you need to write exit.
These are the game rules:
Let's build a fun quiz game in the console!
Build a function constructor called Question to describe a question. A question should include:
a) question itself
b) the answers from which the player can choose the correct one (choose an adequate data structure here, array, object, etc.)
c) correct answer (I would use a number for this)Create a couple of questions using the constructor
Store them all inside an array
Select one random question and log it on the console, together with the possible answers (each question should have a number) (Hint: write a method for the Question objects for this task).
Use the 'prompt' function to ask the user for the correct answer. The user should input the number of the correct answer such as you displayed it on Task 4.
Check if the answer is correct and print to the console whether the answer is correct ot nor (Hint: write another method for this).
Suppose this code would be a plugin for other programmers to use in their code. So make sure that all your code is private and doesn't interfere with the other programmers code (Hint: we learned a special technique to do exactly that).
After you display the result, display the next random question, so that the game never ends (Hint: write a function for this and call it right after displaying the result)
Be careful: after Task 8, the game literally never ends. So include the option to quit the game if the user writes 'exit' instead of the answer. In this case, DON'T call the function from task 8.
Track the user's score to make the game more fun! So each time an answer is correct, add 1 point to the score (Hint: I'm going to use the power of closures for this, but you don't have to, just do this with the tools you feel more comfortable at this point).
Display the score in the console. Use yet another method for this.
var Question = function(question , answer1 , answer2, answer3 , correctAnswer) {
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.correctAnswer = correctAnswer;
this.askQuestion = function() {
console.log(this.question);
console.log(this.answer1);
console.log(this.answer2);
console.log(this.answer3);
}
this.chekQuestion = function(){
var answerQuestion = prompt('Answer the question');
console.log(answerQuestion);
if (answerQuestion == this.correctAnswer ){
console.log(answerQuestion + '--- is correct answer');
scorePlayer ++;
console.log("Your score is ---" + scorePlayer);
initGame()
} else if (answerQuestion == "exit"){
alert('game is stopped');
}else {
console.log(answerQuestion + '--- wrong answer')
console.log("Your score is ---" + scorePlayer);
initGame()
}
}
}
var authorOfCourseQuestion = new Question('Who is an author of course?', '0:John' ,'1:Jane' ,'2:Jonas', '2');
var whatIsJS = new Question('What is JS for you?', '0:fun' ,'1:boring' ,'2:not interesting', '0');
var whatIsAFunctionInJS = new Question('What a function in JS?', '0:string' ,'1:obj' ,'2:number', '1');
var arrayQuestions = [authorOfCourseQuestion , whatIsJS , whatIsAFunctionInJS ];
var scorePlayer = 0;
function initGame() {
var randomNumber = Math.floor(Math.random()*3);
arrayQuestions[randomNumber].askQuestion();
arrayQuestions[randomNumber].chekQuestion();
}
initGame();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Section 5: Advanced JavaScript: Objects and Functions</title>
</head>
<body>
<h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
<script src="script.js"></script>
</body>
</html>