-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcitonConstructorPractice.js
50 lines (45 loc) · 1.39 KB
/
funcitonConstructorPractice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Function constructor
var Question = (function(question, answersArr, correctAnswer){
this.question = question;
this.answersArr = answersArr;
this.correctAnswer = correctAnswer;
this.displayQuestion = function() {
console.log(this.question);
for (var i = 0; i < this.answersArr.length; i++) {
console.log([i] + ': ' + this.answersArr[i]);
}
};
this.checkAnswer = function(userAnswer) {
if (userAnswer == this.correctAnswer){
playerScore = playerScore + 1;
console.log('winner winner chicken dinner. Score: ' + playerScore);
}
else {
console.log('nope');
}
nextQuestion(userAnswer);
}
})
var playerScore = 0;
var question1 = new Question('This is the question', ['This is an answer', 'This is not right'], 0);
var question2 = new Question('The answer is red', ['Green', 'Blue', 'Red'], 2);
var questionsArr = [
question1,
question2,
]
// Function: Select and display random question and prompt for answer
function fireQuestion() {
var randomQuestion= questionsArr[Math.floor(Math.random() * questionsArr.length)];
randomQuestion.displayQuestion();
randomQuestion.checkAnswer(prompt("Please enter answer"));
}
// Function: Fires next question, handles exit.
function nextQuestion(userAnswer) {
if (userAnswer != 'exit') {
fireQuestion();
}
else {
console.log('Game stopped. Final score: ' + playerScore);
}
}
fireQuestion();