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?
Question.prototypebelong there. You can just use variables. \$\endgroup\$