2
\$\begingroup\$

Description:

  • The questions are being showed in the console tap with player's score.
  • It's questioning by prompt continually until the prompt input gets 'exit'.
  • If the answer for each question is right then player's scores are counting up.
  • But, if not then it shows the same question over and over until the answer is right.
var Question = function(question,answers,correctAnswer){
  this.question = question;
  this.answers = answers;
  this.correctAnswer = correctAnswer;
}
Question.prototype.score = 0;
Question.prototype.playersAnswer;

Question.prototype.logAll = function(){
  console.log(this.question);
  for(var i = 0; i < this.answers.length; i++){
    console.log(i + " : " + this.answers[i]);
  }
}
Question.prototype.askQuestions = function(){
    Question.prototype.playersAnswer = prompt(this.question);
}
Question.prototype.checkIfRight = function (){
  if(this.correctAnswer === Question.prototype.playersAnswer){
    console.log("Correct Answer!");
    Question.prototype.score++;
    Question.prototype.logScore();
  }
  else if(Question.prototype.playersAnswer === "exit"){return 0;}
  else {
    console.log("Wrong! Try again :)");
    Question.prototype.logScore();
    return 'w';
  }
}
Question.prototype.logScore = function(){
  console.log("Your Score : " + Question.prototype.score
  + "\n---------------------------------");
}

var q1 = new Question('What is the name of the course\'s teacher?',['John','Baek','Jonas'],'2');
var q2 = new Question('Is JavaScript the coolest language in the world?',['Yes','No'],'0');
var q3 = new Question('What language are you gonna learn about after?',
                      ['C++','C','Java','Python','Node.js'],'4');
var questions = [q1,q2,q3];
function init(){
  var n = Math.floor(Math.random() * 3);
  questions[n].logAll();
  questions[n].askQuestions();
  questions[n].checkIfRight();

}

do {
  var n = Math.floor(Math.random() * 3);
  questions[n].logAll();
  questions[n].askQuestions();
  while(questions[n].checkIfRight() === 'w'){
    questions[n].logAll();
    questions[].askQuestions();
  };
}
while (q1.playersAnswer !== "exit");

Can you review it for best coding practices?

\$\endgroup\$
2
  • \$\begingroup\$ You can instal ESLint in your project, ESLint is a standard used by the biggest companies which use JavaScript. ESLint will throw warnings and errors which will tell you how to format and write your code by the standard, gives you suggestions and links with examples how is wrong and how is right to write your code and format it. You can learn more here: eslint.org I hope that helps, my JavaScript improved a lot after I started using the linting feature in all of my projects. \$\endgroup\$ – Todor Dimov Apr 13 '19 at 4:40
  • \$\begingroup\$ None of the things you’re changing at runtime on Question.prototype belong there. You can just use variables. \$\endgroup\$ – Ry- Apr 13 '19 at 4:47
2
\$\begingroup\$

In object-oriented design, the key to modelling is to figure out how your application will respond to messages. Messages can be requests for information, requests to do something, or notifications that something has happened.

In your example, the application could receive a message, "what is the next question?" After giving it some thought, you might decide that a Quiz object would be responsible for providing that information, in which case you would need Quiz class with a method to respond to the message.

function Quiz() {
...
}

Quiz.prototype.nextQuestion = function () {
...
};

Now, you don't really have any questions yet, so another obvious message that your application will have to handle is, "add this question with these answer choices and correct answer."

DO NOT simply rush and add a new method to Quiz to handle the new message; instead, you need to repeat the thought process and decide which object is best suited to respond. In this case, again it is a Quiz object because questions are needed to answer the previous message of, "what is the next question?" So, you update your Quiz class accordingly. (Along the way you create a structure to represent a question and an index to track the current question, thus enabling you to find the next question.)

function Quiz() {
    this.questions = [];
    this.index = 0;
}

Quiz.prototype.addQuestion = function (text, choices, answer) {
   this.questions.push(new Question(text, choices, answer)); 
};

Quiz.prototype.nextQuestion = function () {
    if (this.index < this.questions.length)
        return this.questions[this.index++];
    return null;
};

function Question(text, choices, answer) {
    this.text = text;
    this.choices = choices;
    this.answer = answer;
}

I will skip messages that your application might have to handle in relation to player management and get to those that will require your objects to collaborate.

Say, your application receives a message, "the player has answered to this question with this answer." Going through the same thought process as before, you decide that the Quiz object will handle that, and you add a new method.

Quiz.prototype.answer = function (player, question, answer) {
...
};

However, this time, you need to check if the answer is correct as part of responding to that message. But which object can do that? Of course, the Question structure has the knowledge of correct answer, so it becomes its responsibility. Question has just been promoted from a simple structure to a full-blown class that can also respond to messages; in this case, "is this answer correct?" Therefore--

Question.prototype.isCorrect = function (answer) {
    return this.answer === answer;
};

And--

Quiz.prototype.answer = function (player, question, answer) {
    if (question.isCorrect(answer)) {
         // score some points for player
    }
}

As you can see, object-oriented design is not just finding classes and adding methods to them. You need to think about the messages and how they are handled; in other words, messages are key--NOT classes, NOT methods.

Good luck!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy