Need help in JavaScript Quiz as it cant run

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

var score = 0;

var selectedCategory = [ ];

var Name = input.question('Please Enter Your Name:')

class Quiz {

  constructor(questions) {

    this.questions = [];

    this.questions.push(new MCQ("What Is My Favourite Movie Series", ["Star Wars", "IP Man", "Lord of the Rings ", "None of the above"], 0, 1));

    this.questions.push(new MCQ("When Was Our First Class Movie Outing? ", ["2011", "2012", "2013", "None of the above"], 0, 1));

    this.questions.push(new MCQ("When Did We Watch Our first Movie At ? ", ["Cathay", "Shaw", "GoldenV", "None of the above"], 0, 1));

    this.questions.push(new MCQ("When Was My First Movie That I Watched ? ", ["Jurassic Park", "Goblin", "Oreo", "None of the above"], 0, 1));

    this.questions.push(new MCQ("Who Did I Watched My First Movie With ? ", ["Parents", "Crush", "Girlfriend", "None of the above"], 0, 1));

    this.questions.push(new MCQ("What Is My Favourite Food Dish", ["Chicken Rice", "Wonton Mee", "Laksa ", "None of the above"], 0, 2));

    this.questions.push(new MCQ("What Is My Favourite Food Genre", ["Chinese", "Malay", "Indian ", "None of the above"], 0, 2));

    this.questions.push(new MCQ("What Do I Eat For Breakfast", ["Prata", "Prata", "Prata ", "None of the above"], 0, 2));

    this.questions.push(new MCQ("What Is My Favourite Food Carbo", ["Rice", "Noodles", "Soup ", "None of the above"], 0, 2));

    this.questions.push(new MCQ("What Is My Favourite Food Series", ["Gordan Ramsay", "Foodking", "Idk ", "None of the above"], 0, 2));

    this.questions.push(new MCQ("What Is My Favourite Music Video", ["Gordan Ramsay", "Blank Space", "Gangnam Style ", "None of the above"], 0, 3));

    this.questions.push(new MCQ("What Is My Favourite Music Genre", ["POP", "Jazz", "HipHop", "None of the above"], 0, 3));

    this.questions.push(new MCQ("What Is My Favourite Song", ["Buttercup", "National Anthem", "Lovers ", "None of the above"], 0, 3));

    this.questions.push(new MCQ("What Is My Favourite Artist", ["Gordan Ramsay", "Taylor Swift", "Lauv ", "None of the above"], 0, 3));

    this.questions.push(new MCQ("What Is My Most Hated Song", ["Gordan Ramsay", "Baby", "Idk ", "None of the above"], 0, 3));

    this.questions.push(new MCQ("What Is My Favourite Subject In Primary School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

    this.questions.push(new MCQ("What Is My Favourite Subject In Secondary School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

    this.questions.push(new MCQ("What Is My Favourite Subject In ITE School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

    this.questions.push(new MCQ("What Is My Favourite Subject In Polytechnic", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

    this.questions.push(new MCQ("What Is My GPA Now", ["1", "2", "3 ", "None of the above"], 0, 4));

  }

  getMaxCategory() {

    var max;

    for (let i = 0; i < this.questions.length; i++) {

        if (this.questions[0].category < this.questions[i].category) {

            max = this.questions[i].category;

        }

    }

    return max;

  }

  CheckIfInArray(value, array) {

      return array.indexOf(value) > -1

  }

  getAnswerScreen() {

      var string = '';

      for (let i = 0; i < ansArr.length; i++) {

          string += ('Your answer for Qn ' + (i + 1) + ': (' + ansArr[i] + ')' + '\n');

      }

      return string;

  }

  getScore() {

      for (let i = 0; i < selectedCategory.length; i++) {

          if (ansArr[i] == (selectedCategory[i].answer + 1)) {

              score++

          }

      }

      return score;

  }

  getQuiz() {

    do {

      var response = parseInt(input.question('>> '));

      if (Number.isInteger(response) == false || response > this.getMaxCategory()) {

        console.log('-----------------------------------------------------------')

        console.log('Please enter a valid number');

        console.log('-----------------------------------------------------------')

      }

    } while (Number.isInteger(response) == false || response > this.getMaxCategory());

    switch (response) {

      case 1:

        console.log('----------------------------------------------------------')

        console.log('You have selected the Movie Category');

        console.log('----------------------------------------------------------')

        break;

      case 2:

        console.log('----------------------------------------------------------')

        console.log('You have selected the FOOD category');

        console.log('----------------------------------------------------------')

        break;

      case 3:

        console.log('----------------------------------------------------------')

        console.log('You have selected the Music category');

        console.log('----------------------------------------------------------')

        break;

      case 4:

        console.log('----------------------------------------------------------')

        console.log('You have selected the Education category');

        console.log('----------------------------------------------------------')

        break;

        for (let i = 0; i < this.questions.length; i++) {

          if (this.questions[i].category == response) {

              selectedCategory.push(this.questions[i]);

          }

        for (let i = 0; i < selectedCategory.length; i++) {

          var y = selectedCategory[i];

          console.log('--------------------------------------------------------------------');

          console.log('Question ' + (i + 1) + ': ' + y.getQuestion());  

        }

      }

    }  

  }

}

class MCQ {

  constructor(question, answer, choices, category) {

    this.questions = question;

    this.choice = choices;

    this.answer = answer;

    this.category = category;

  }

  getQuestion() {

    var x = "";

    for (let i = 0; i < this.choice.length; i++) {

      x += '(' + (i + 1) + ')' + ' ' + this.choice[i] + "\n";

    }

    return this.questions + "\n" + x;

  }

}

console.log("-= Welcome to How Well You know ME Application =-")

console.log('Hi ' + Name + ', Please choose the catogery you would like to try:' + '\n' + '(1) Movie' + '\n' + '(2) Music' + '\n' + '(3) FOOD' + '\n' + '(4) Education');

var quiz = new Quiz();

console.log(quiz.getQuiz());

Hi Casilb,

What are you expecting this code to do?
What does it do instead?
Could you create a jsfiddle or codepen that reproduces the issue?

i am tasked to create a quiz in visual studio code.

To get the user to input their name -> choose which quiz option they want to do (1,2,3,4)

Gets the user to do all the 5 questions in that option they selected
After user attempts the quiz, score will be displayed out of /5
But i have no idea whats wrong with my coding…

I have managed to get the quiz but it gets into an infinite loop when the user completed the 5 questions. by right it should be displaying the score when user keys 0 to submit or 1-5 to change their answer.

var input = require(‘readline-sync’);

var score = 0;

var selectedCategory = ;

var ansArr = ;

var Name = input.question(‘Please Enter Your Name:’);

class Quiz {

constructor(questions) {

this.questions = [];

this.questions.push(new MCQ("What Is My Favourite Movie Series", ["Star Wars", "IP Man", "Lord of the Rings ", "None of the above"], 0, 1));

this.questions.push(new MCQ("When Was Our First Class Movie Outing? ", ["2011", "2012", "2013", "None of the above"], 0, 1));

this.questions.push(new MCQ("When Did We Watch Our first Movie At ? ", ["Cathay", "Shaw", "GoldenV", "None of the above"], 0, 1));

this.questions.push(new MCQ("When Was My First Movie That I Watched ? ", ["Jurassic Park", "Goblin", "Oreo", "None of the above"], 0, 1));

this.questions.push(new MCQ("Who Did I Watched My First Movie With ? ", ["Parents", "Crush", "Girlfriend", "None of the above"], 0, 1));

this.questions.push(new MCQ("What Is My Favourite Food Dish", ["Chicken Rice", "Wonton Mee", "Laksa ", "None of the above"], 0, 2));

this.questions.push(new MCQ("What Is My Favourite Food Genre", ["Chinese", "Malay", "Indian ", "None of the above"], 0, 2));

this.questions.push(new MCQ("What Do I Eat For Breakfast", ["Prata", "Prata", "Prata ", "None of the above"], 0, 2));

this.questions.push(new MCQ("What Is My Favourite Food Carbo", ["Rice", "Noodles", "Soup ", "None of the above"], 0, 2));

this.questions.push(new MCQ("What Is My Favourite Food Series", ["Gordan Ramsay", "Foodking", "Idk ", "None of the above"], 0, 2));

this.questions.push(new MCQ("What Is My Favourite Music Video", ["Gordan Ramsay", "Blank Space", "Gangnam Style ", "None of the above"], 0, 3));

this.questions.push(new MCQ("What Is My Favourite Music Genre", ["POP", "Jazz", "HipHop", "None of the above"], 0, 3));

this.questions.push(new MCQ("What Is My Favourite Song", ["Buttercup", "National Anthem", "Lovers ", "None of the above"], 0, 3));

this.questions.push(new MCQ("What Is My Favourite Artist", ["Gordan Ramsay", "Taylor Swift", "Lauv ", "None of the above"], 0, 3));

this.questions.push(new MCQ("What Is My Most Hated Song", ["Gordan Ramsay", "Baby", "Idk ", "None of the above"], 0, 3));

this.questions.push(new MCQ("What Is My Favourite Subject In Primary School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

this.questions.push(new MCQ("What Is My Favourite Subject In Secondary School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

this.questions.push(new MCQ("What Is My Favourite Subject In ITE School", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

this.questions.push(new MCQ("What Is My Favourite Subject In Polytechnic", ["Math", "English", "Chinese ", "None of the above"], 0, 4));

this.questions.push(new MCQ("What Is My GPA Now", ["1", "2", "3 ", "None of the above"], 0, 4));

}

getMaxCategory() {

var max;

for (let i = 0; i < this.questions.length; i++) {

  if (this.questions[0].category < this.questions[i].category) {

    max = this.questions[i].category;

  }

}

return max;

}

CheckIfInArray(value, array) {

return array.indexOf(value) > -1

}

getAnswerScreen() {

var string = '';

for (let i = 0; i < ansArr.length; i++) {

  string += ('Your answer for Qn ' + (i + 1) + ': (' + ansArr[i] + ')' + '\n');

}

return string;

}

getScore() {

for (let i = 0; i < selectedCategory.length; i++) {

  if (ansArr[i] == (selectedCategory[i].answer + 1)) {

    score++

  }

}

return score;

}

getQuiz() {

do {

  var response = parseInt(input.question('>> '));

  if (Number.isInteger(response) == false || response > this.getMaxCategory()) {

    console.log('-----------------------------------------------------------')

    console.log('Please enter a valid number');

    console.log('-----------------------------------------------------------')

  }

} while (Number.isInteger(response) == false || response > this.getMaxCategory());

switch (response) {

  case 1:

    console.log('----------------------------------------------------------')

    console.log('You have selected the Movie Category');

    console.log('----------------------------------------------------------')

    break;

  case 2:

    console.log('----------------------------------------------------------')

    console.log('You have selected the FOOD category');

    console.log('----------------------------------------------------------')

    break;

  case 3:

    console.log('----------------------------------------------------------')

    console.log('You have selected the Music category');

    console.log('----------------------------------------------------------')

    break;

  case 4:

    console.log('----------------------------------------------------------')

    console.log('You have selected the Education category');

    console.log('----------------------------------------------------------')

    break;

}

for (let i = 0; i < this.questions.length; i++) {

  if (this.questions[i].category == response) {

    selectedCategory.push(this.questions[i]);

  }

}

// printing the quiz

for (let i = 0; i < selectedCategory.length; i++) {

  var y = selectedCategory[i];

  console.log('--------------------------------------------------------------------');

  console.log('Question ' + (i + 1) + ': ' + y.getQuestion());

  console.log(y.getChoices());

  var userAnswer = input.question(">> ")

  ansArr.push(userAnswer)

}

}

endQuiz() {

var quizEnd = 0

do {

  quizEnd = input.question("Please enter 0 to submit or [1 to " + selectedCategory.length + "] to change your answer.")

  parseInt(quizEnd)

} while (Number.isInteger(quizEnd) == false || 0 > quizEnd || quizEnd > selectedCategory.length)



switch (quizEnd) {

  case 0:

    console.log("submit");

    break;

  case 1:

    console.log("one");

    break;

  case 2:

    console.log("two");

    break;

  case 3:

    console.log("three");

    break;

  case 4:

    console.log("four");

    break;

  case 5:

    console.log("five");

    break;

}

}

}

class MCQ {

constructor(question, answer, choices, category) {

this.questions = question;

this.choice = choices;

this.answer = answer;

this.category = category;

}

getQuestion() {

return this.questions;

}

getChoices() {

var x = "";

for (let i = 0; i < this.answer.length; i++) {

  x += '(' + (i + 1) + ')' + ' ' + this.answer[i] + '\n';

}

return x

}

}

console.log("-= Welcome to How Well You know ME Application =-")

console.log('Hi ’ + Name + ‘, Please choose the catogery you would like to try:’ + ‘\n’ + ‘(1) Movie’ + ‘\n’ + ‘(2) Music’ + ‘\n’ + ‘(3) FOOD’ + ‘\n’ + ‘(4) Education’);

var quiz = new Quiz();

// start the quiz

quiz.getQuiz();

// after quiz has finished, ask if user wants to submit or edit ans

quiz.endQuiz();

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums