Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Section 2: JavaScript Language Basics</title>
<title>Section 5: Advanced JavaScript: Objects and Functions</title>
</head>

<body>
<h1>Section 2: JavaScript Language Basics</h1>
<h1>Section 5: Advanced JavaScript: Objects and Functions</h1>
<script src="script.js"></script>
</body>
</html>
49 changes: 49 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function () {
const Question = function(question, answers, correctAnswer) {
this.question = question;
this.answers = answers;
this.correctAnswer = correctAnswer;
}

const skyQuestion = new Question('What color is the sky?', ['blue', 'red', 'green'], 0);
const codingQuestion = new Question('Whats the best programming language?', ['Java', 'C', 'Javascript', 'Python'], 2);
const variableQuesion = new Question('What kind of variable should you use if you won\'t reassign a new value to it?', ['var', 'let', 'const'], 2);
const allQuestions = [skyQuestion, codingQuestion, variableQuesion];

let score = 0;

(function display(questions) {
const random = Math.floor(Math.random() * questions.length);

console.log(questions[random].question);
displayAnswers(questions[random].answers);

const message = prompt(`${questions[random].question} (Type 'exit' to end the game)`, 'Insert the number of the correct answer');

if (message == 'exit') {
console.log(`End of the game! Your final score is ${score}`);
}
else if (message == questions[random].correctAnswer) {
score++;
console.log(`Correct! Your current score is ${score}`);
display(questions);
}
else if (!isNaN(message)) {
console.log(`Incorrect! Your current score is ${score}`);
display(questions);
}
else {
console.log('Invalid input');
display(questions);
}

})(allQuestions);

function displayAnswers(answers) {
let count = 0;
answers.forEach(answer => {
console.log(`${count} - ${answer}`);
count++;
});
}
})();