Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cae359e

Browse files
author
Samane
authored
Add files via upload
1 parent a81968f commit cae359e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Challenge7/app.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(function () {
2+
var score;
3+
4+
function Question(question, answers, correct) {
5+
this.question = question;
6+
this.answers = answers;
7+
this.correct = correct;
8+
}
9+
10+
Question.prototype.displayQuestion = function () {
11+
console.log(this.question);
12+
for (var i = 0; i < this.answers.length; i++)
13+
console.log((i + 1) + '. ' + this.answers[i]);
14+
}
15+
16+
Question.prototype.checkAnswer = function (answer) {
17+
if (answer === this.correct) {
18+
console.log('Correct!');
19+
score++;
20+
console.log('Score: ' + score);
21+
} else {
22+
console.log('incorrect. Try again! :)');
23+
console.log('Score: ' + score);
24+
}
25+
}
26+
var question_1 = new Question('Is JavaScript the coolest programming language in the world?', ['Yes', 'No'], 1);
27+
var question_2 = new Question('How many days are 1 week?', ['5days', '9days', '7days', '11days'], 3);
28+
var arr = [question_1, question_2];
29+
score = 0;
30+
makeQuestion();
31+
32+
function makeQuestion() {
33+
var num = Math.floor(Math.random() * arr.length);
34+
arr[num].displayQuestion();
35+
var answer = prompt('Enter the number of the correct answer');
36+
if (answer !== 'exit') {
37+
arr[num].checkAnswer(parseInt(answer));
38+
console.log('------------');
39+
makeQuestion();
40+
}
41+
}
42+
})();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /