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!

  1. 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)

  2. Create a couple of questions using the constructor

  3. Store them all inside an array

  4. 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).

  5. 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.

  6. Check if the answer is correct and print to the console whether the answer is correct ot nor (Hint: write another method for this).

  7. 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).

  8. 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)

  9. 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.

  10. 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).

  11. 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() {
        this.chekQuestion = function(){
            var answerQuestion = prompt('Answer the question');
            if (answerQuestion ==  this.correctAnswer ){
                console.log(answerQuestion + '--- is correct answer');
                scorePlayer ++;
                console.log("Your score is ---" + scorePlayer);
            } else if (answerQuestion == "exit"){
                alert('game is stopped');

            }else {
                console.log(answerQuestion + '--- wrong answer')
                console.log("Your score is ---" + scorePlayer);



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);

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8">
        <title>Section 5: Advanced JavaScript: Objects and Functions</title>

        <h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
        <script src="script.js"></script>


1 Answer 1


Your layout is all over the place. Sometimes, you indent by 8 spaces, sometimes by 4. Sometimes, you have whitespace around else on both sides, sometimes only on the right. Sometimes, you have whitespace around , on both sides, sometimes only on the right. Sometimes, you have one space before and after an operator, sometimes none, sometimes, you have two spaces after an operator. Be consistent!

Consistency is important, because if you express the same thing in two different ways, a reader of the code will think that you wanted to express two different things, and they will waste time wondering what the difference is.

You have empty lines to break your code up into logical units, but they are in weird places, such as before an else or before a closing curly brace.

Sometimes, you use semicolon, sometimes you don't.

You should be consistent and follow a fixed set of rules. Ideally, you should use an automated tool to format your code, so that you don't have to make those decisions at all.

For example, jslint with default settings detects 3 errors and then stops and doesn't even parse the whole file, jshint with the default settings detects 6 errors, and eslint with some reasonable settings detects a whopping 63 errors and 14 warnings. With my settings, the numbers are even worse: 20 errors for jshint and 155(!!!) for eslint. You should always use a linter and make sure that your code is lint-clean.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.