Sei sulla pagina 1di 24

var readline = require('readline-sync');

/* ============================== */

// Class

class MCQ {

constructor(question, option, answer, category) {

this.question = question;

this.options = option;

this.answer = answer;

this.category = category;

displayQuestion() {

var result = "";

result += this.question

result += "\n"

for (var i = 0; i < 4; i++) {

result += "(" + (i + 1) + ") " + this.options[i] + "\n";


}

return result;

displayHelpPassQuestion() {

do {

var i = Math.floor((Math.random() * 3) + 1);

} while (i == this.answer);

var result = "";

result += this.question

result += "\n"

if (i < this.answer) {

result += "(" + (i + 1) + ") " + this.options[i] + "\n";

result += "(" + (this.answer + 1) + ") " + this.options[this.answer] + "\n";

} else {

result += "(" + (this.answer + 1) + ") " + this.options[this.answer] + "\n";


result += "(" + (i + 1) + ") " + this.options[i] + "\n";

return result

displayQuestionOnly() {

var result = "";

result += this.question

return result;

displayOptionOnly(i) {

var result = "";

result += this.options[i]

return result;
}

validateAnswer() {

var result = "";

result += this.answer + 1

return result;

class Quiz {

constructor() {

this.quizQuestions = [];

this.quizQuestions.push(new MCQ("Which is the LONGEST river in the world?", ["Amazon River", "Nile", "Huang He", "Yangtze"], 1, 1));

this.quizQuestions.push(new MCQ("Which is the SECOND largest country in terms of land area?", ["United State", "China", "Canada", "Russia"], 2, 1));

this.quizQuestions.push(new MCQ("Who is the first person to walk on the moon?", ["Buzz Aldrin", "Pete Conrad", "Alan Bean", "Neil Armstrong"], 3,
1));

this.quizQuestions.push(new MCQ("Which is the smallest ocean in the world?", ["Pacific Ocean", "Atlantic Ocean", "Arctic Ocean", "Indian Ocean"], 2,
1));
this.quizQuestions.push(new MCQ("Who is the first president of the United State?", ["George Washington", "Abraham Lincoln", "Barack Obama",
"Donald Trump"], 0, 1));

this.quizQuestions.push(new MCQ("Hyper Text Markup Language Stand For?", ["JavaScript", "XHTML", "CSS", "HTML"], 3, 2));

this.quizQuestions.push(new MCQ("Which language is used for styling web pages?", ["HTML", "JQuery", "CSS", "XML"], 2, 2));

this.quizQuestions.push(new MCQ("Which is not a JavaScript Framework?", ["Python Script", "JQuery", "Django", "NodeJS"], 2, 2));

this.quizQuestions.push(new MCQ("Which is used for Connect To Database?", ["PHP", "HTML", "JS", "All"], 0, 2));

this.quizQuestions.push(new MCQ("What is the name given to a network of servers that deliver information and is entirely on the internet and is
accessed through web browsers", ["World Wide Web", "Cloud", "Internet", "Network"], 1, 2));

this.quizQuestions.push(new MCQ("Which 2019 hit contains the lyrics 'I got the horses in the back / Horse tack is attached / Hat is matte black / Got
the boots that's black to match'?", ["Old Town Road by Lil Nas X", "Dark Horse by Katy Perry", "Black Horse and the Cherry Tree by KT Tunstall", "Just Like
Them Horses by Reba McEntire"], 0, 3));

this.quizQuestions.push(new MCQ("Ed Sheeran teamed up with which American superstar on the May hit 'I Don't Care'?", ["Arianna Grande", "Selena
Gomez", "Justin Bieber", "Calvin Harris"], 2, 3));

this.quizQuestions.push(new MCQ("Sophie Turner appeared in the video of which Jonas Brothers single?", ["Sucker", "Pom Poms", "Cool", "Old Town
Road"], 0, 3));

this.quizQuestions.push(new MCQ("Calvin Harris teamed up with which English singer for to top the charts with the single 'Promises'?", ["Sam Smith",
"Drake", "Dua Lipa", "Cardi B"], 0, 3));

this.quizQuestions.push(new MCQ("The soundtrack to which Hugh Jackman and Zac Efron film spent weeks at the top of the British Album charts in
early 2018?", ["A Star Is Born", "Spider-Man: Into the Spider-Verse", "Black Panther", "The Greatest Showman: Reimagined"], 3, 3));

this.quizQuestions.push(new MCQ("Which striker scored the first hat-trick of the 2018/19 Premier League season against Huddersfield Town?",
["Mohamed Salah", "Sergio Agüero", "Harry Kane", "Raheem Sterling"], 1, 4));

this.quizQuestions.push(new MCQ("Maurizio Sarri replaced which fellow countryman in charge at Chelsea?", ["Antonio Conte", "Unai Emery", "Pep
Guardiola", "Javi Gracia"], 0, 4));
this.quizQuestions.push(new MCQ("From which sport does the term 'down to the wire' originate?", ["Darts", "Horse Racing", "Ice Hockey", "Football"],
1, 4));

this.quizQuestions.push(new MCQ("How many players are there on a Baseball team?", ["9", "5", "11", "7"], 0, 4));

this.quizQuestions.push(new MCQ("Which country is the winner of the Fifa World Cup 2018", ["Germany", "Brazil", "England", "France"], 3, 4));

getNumberOfQuestions() {

return this.quizQuestions.length;

getQuestionAt(index) {

return this.quizQuestions[index];

/* ============================== */

// Function

// Function for selection

function selection() {
userSelection = readline.question(">> ");

return userSelection;

// Function for choice

function userInputAnswer() {

choice = readline.question(">> ");

return choice;

// build quiz according to the category user select

function buildQuiz() {

switch (userSelectionList[0]) {

case '1': for (i = 0; i < 5; i++) {

displayQuestionList.push(myQuiz.getQuestionAt(i));

};

break;

case '2': for (i = 5; i < 10; i++) {

displayQuestionList.push(myQuiz.getQuestionAt(i));

};
break;

case '3': for (i = 10; i < 15; i++) {

displayQuestionList.push(myQuiz.getQuestionAt(i));

};

break;

case '4': for (i = 15; i < 20; i++) {

displayQuestionList.push(myQuiz.getQuestionAt(i));

};

break;

case '5': for (index = 0; index < 5; index++) {

var i = Math.floor((Math.random() * 20) + 1);

displayQuestionList.push(myQuiz.getQuestionAt(i));

};

function getNumberOfDisplayQuestions() {

return displayQuestionList.length;

}
function getDisplayQuestionAt(index) {

return displayQuestionList[index];

// Function for display quiz

function displayQuiz() {

var index = 0;

do {

var q = getDisplayQuestionAt(index);

var result = ""

result += "\n" // Leave a space line

result += "-----------------------------\n"

result += "Question " + (index + 1) + " of " + getNumberOfDisplayQuestions() + "\n"

result += "-----------------------------\n"

result += q.displayQuestion();

result += "-----------------------------"

console.log(result);

userInputAnswer();
do {

if (choice != "1" && choice != "2" && choice != "3" && choice != "4") {

if (choice == "") {

console.log("ALERT! You leave the question blank.\nWould you like to complete the question before process to next question.")

console.log("-----------------------------");

console.log("Press Y to answer the question / Press N to skip the question")

selection();

do {

if (userSelection != "Y" && userSelection != "y" && userSelection != "N" && userSelection != "n") {

console.log(""); // Leave a space line

console.log("ERROR! Press Y to answer the question / Press N to skip the question");

selection();

} // validator

if (userSelection == "Y" || userSelection == "y") {

// Help the user when the user face problem in answering current question

if (helpPass == 1 || helpPass == 2) {

console.log("Press Y to get help / Press N to reject");

selection();

do {
if (userSelection != "Y" && userSelection != "y" && userSelection != "N" && userSelection != "n") {

console.log(""); // Leave a space line

console.log("ERROR! Press Y to get help / Press N to reject")

selection();

} // validator

if (userSelection == "Y" || userSelection == "y") {

helpPass--;

var q = getDisplayQuestionAt(index);

var result = ""

result += "\n" // Leave a space line

result += "-----------------------------\n"

result += "Question " + (index + 1) + " of " + getNumberOfDisplayQuestions() + "\n"

result += "-----------------------------\n"

result += q.displayHelpPassQuestion();

result += "-----------------------------"

console.log(result);

do {
userInputAnswer();

if (choice != "1" && choice != "2" && choice != "3" && choice != "4") {

console.log("ERROR! You must answer the question when you using Help Pass.")

} // validator

} while (choice != "1" && choice != "2" && choice != "3" && choice != "4")

} else if (userSelection == "N" || userSelection == "n") {

console.log("-----------------------------");

console.log("Please select the choices provided:");

userInputAnswer();

} while (userSelection != "Y" && userSelection != "y" && userSelection != "N" && userSelection != "n")

} else {

console.log("Sorry, the remaining Help Pass already being used just now. You unable to get any more help.");

console.log("-----------------------------");

console.log("Please select the choices provided:");

userInputAnswer();

} while (userSelection != "Y" && userSelection != "y" && userSelection != "N" && userSelection != "n")

} else if (choice != "") {


console.log("-----------------------------");

console.log("ERROR! You can only choose the choices above.\nPlease select the choices provided:");

userInputAnswer();

} // validator

} while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "");

userAnswerList[index] = choice;

console.log("Your selected answer: " + choice);

console.log("-----------------------------");

console.log("(Press N for next question, P for previous question, X to exit)");

selection();

do {

if (userSelection != "N" && userSelection != "n" && userSelection != "P" && userSelection != "p" && userSelection != "X" && userSelection != "x") {

console.log("-----------------------------");

console.log("ERROR! Press N for next question, P for previous question, X to exit");

selection();

} // validator

if (userSelection == "N" || userSelection == "n") {

if (index != getNumberOfDisplayQuestions() - 1) {
index++;

else if (index == getNumberOfDisplayQuestions() - 1) {

unattemptCheck();

console.log("-----------------------------\n");

console.log("Congratulations!!! You are successfully finished the quiz.\n");

do {

console.log("Please press x to exit and see your result.");

selection()

} while (userSelection != "X" && userSelection != "x")

else if (userSelection == "P" || userSelection == "p") {

if (index != 0) {

index--;

} while (userSelection != "N" && userSelection != "n" && userSelection != "P" && userSelection != "p" && userSelection != "X" && userSelection != "x")

} while (userSelection != "X" && userSelection != "x")


if (index != getNumberOfDisplayQuestions() - 1) {

console.log("You haven't finished the quiz\nWould you like to continue or quit the quiz");

console.log("Press C to continue / Press Q to quit");

selection();

do {

if (userSelection != "C" && userSelection != "c" && userSelection != "Q" && userSelection != "q") {

console.log("ERROR! Press C to continue / Press Q to quit");

selection();

} // validator

else

if (userSelection == "C" || userSelection == "c") {

displayQuiz();

} else if (userSelection == "Q" || userSelection == "q") {

showResult();

} while (userSelection != "C" && userSelection != "c" && userSelection != "Q" && userSelection != "q")

} else if (index == getNumberOfDisplayQuestions() - 1) {

console.log("-----------------------------\n");

console.log("Congratulations!!! You are successfully finished the quiz.\n");

showSelectedAnswers();
}

// Function for checking unattempt question

function unattemptCheck() {

var unattemptQuestion = 0;

for (var index = 0; index < userAnswerList.length; index++) {

if (userAnswerList[index] == "") {

unattemptQuestion++;

console.log("\n"); // Leave a space line

console.log("You have " + unattemptQuestion + " question/questions unattempted\nPlease answer the question/questions below.");

for (var index = 0; index < userAnswerList.length; index++) {

if (userAnswerList[index] == "") {

var q = getDisplayQuestionAt(index);

var result = ""

result += "\n" // Leave a space line

result += "-----------------------------\n"
result += "Question " + (index + 1) + " of " + getNumberOfDisplayQuestions() + "\n"

result += "-----------------------------\n"

result += q.displayQuestion();

result += "-----------------------------"

console.log(result);

userInputAnswer();

do {

if (choice != "1" && choice != "2" && choice != "3" && choice != "4") {

console.log("ERROR! You must answer the question before you want to submit and get your result.")

userInputAnswer();

} // validator

} while (choice != "1" && choice != "2" && choice != "3" && choice != "4")

userAnswerList[index] = choice;

console.log("Your selected answer: " + choice);

// Function for display summarry of answer selected by user


function showSelectedAnswers() {

do {

var result = "";

result += "\n" //Leave a space line

result += "Here is summary of your answers:\n";

for (var i = 0; i < 5; i++) {

result += "\n"

result += (i + 1) + ".)" + getDisplayQuestionAt(i).displayQuestionOnly()

result += "\n"

result += "Answer: " + "(" + userAnswerList[i] + ")" + " " + getDisplayQuestionAt(i).displayOptionOnly(userAnswerList[i] - 1)

result += "\n"

console.log(result);

do {

if(userSelection != "0" && userSelection != "1" && userSelection != "2" && userSelection != "3" && userSelection != "4" && userSelection != "5") {

console.log("ERROR!")

console.log("Press 0 to submit your answers / Press [1 to 5] to select the question you would like to change your answer.");

selection();

} while (userSelection != "0" && userSelection != "1" && userSelection != "2" && userSelection != "3" && userSelection != "4" && userSelection != "5")
console.log(""); //Leave a space line

if (userSelection != 0) {

console.log("Please select a new answer for question " + userSelection);

switch (userSelection) {

case '1': userAnswerList[userSelection - 1] = userInputAnswer();

break;

case '2': userAnswerList[userSelection - 1] = userInputAnswer();

break;

case '3': userAnswerList[userSelection - 1] = userInputAnswer();

break;

case '4': userAnswerList[userSelection - 1] = userInputAnswer();

break;

case '5': userAnswerList[userSelection - 1] = userInputAnswer();

} while (userSelection != 0);

showResult();

}
// Function for showing the result

function showResult() {

var score = 0;

for (var i = 0; i < 5; i++) {

if (userAnswerList[i] == (getDisplayQuestionAt(i).validateAnswer())) {

score++;

var result = "";

result += "\n"

result += "-----------------------------\n"

result += "Your score: " + score

result += "\n"

console.log(result);

switch (score) {

case 0: console.log("Looks like you must work harder. Try again next time. ^_^\n");

break;
case 1: console.log("Looks like you must work harder. Try again next time. ^_^\n");

break;

case 2: console.log("Looks like you must work harder. Try again next time. ^_^\n");

break;

case 3: console.log("Well done. You almost get full mark. Try again next time. ^_^\n");

break;

case 4: console.log("Well done. You almost get full mark. Try again next time. ^_^\n");

break;

case 5: console.log("Congratulations!!! You get full mark in this quiz. ^_^\n");

/* ============================== */

// Array

// List of user selection

var userSelectionList = [];

// List of display question

var displayQuestionList = [];


// List of user answers

var userAnswerList = [];

/* ============================== */

// Variable

var myQuiz = new Quiz();

var helpPass = 2;

/* ============================== */

// MAIN CODE START HERE

/*
===============================================================================================================================
======================= */

// Open

console.log("-----------------------------");

console.log("WELCOME TO QUIZ APPLICATION");

console.log("-----------------------------");
// Enter User details

var name = readline.question("Please enter your name: ");

do {

// Welcome

console.log("\n"); //Leave a space line

console.log("Hi " + name + ", please choose the quiz category you would like to attempt:");

var category = ["GENERAL KNOWLEDGE", "IT", "MUSIC", "SPORT", "ALL"];

for (var i = 0; i < category.length; i++) {

console.log("(" + (i + 1) + ")" + category[i]);

console.log("-----------------------------");

// Category Selection

selection();

console.log("-----------------------------");

while (!(userSelection > 0 && userSelection < 6)) {

console.log("ERROR! You can only choose the options above.\nPlease select the options provided:");

selection();
console.log("-----------------------------");

} console.log(category[userSelection - 1] + " category selected.\n!!!Instructions!!!\nThis quiz consists of 5 multiple choice questions, and you are to
answer ALL the questions.\nEach question carrys ONE mark.\nIn the quiz, you can press N to proceed to the next question and P to the previous
question.\nGood Luck.\n");

userSelectionList[0] = userSelection;

// Start the quiz

console.log("Press N to start the quiz / Press P to select a new category")

selection();

while (!(userSelection == "N" || userSelection == "n" || userSelection == "P" || userSelection == "p")) {

console.log("-----------------------------");

console.log("Error! Please type the correct key.");

selection();

} while (userSelection == "P" || userSelection == "p");

// Build a list of questions based on the category selected by the user

buildQuiz();

// Display out the questions

displayQuiz();

Potrebbero piacerti anche