Build a Quiz Game

const questions = [
  {
    category: 'General Knowledge',
    question: 'Who painted the Mona Lisa?',
    choices: [
      'Leonardo da Vinci', 
      'Jupiter',
      'Michael Angelo'
    ],
    answer: 'Leonardo da Vinci'
  },
  {
    category: 'Science',
    question: 'What is the process by which plants make their own food?',
    choices: [
      'Photosynthesis', 
      'Evaporation',
      'Kinetic Energy'
    ],
    answer: 'Photosynthesis'
  },
  {
    category: 'History',
    question: 'What year did World War II end?',
    choices: [
      '1950', 
      '1945',
      '1943'
    ],
    answer: '1945'
  },
  {
    category: 'Pop Culture',
    question: 'What is the name of the fictional city where the Batman comics take place?',
    choices: [
      'Vice City', 
      'San Andreas',
      'Gotham City'
    ],
    answer: 'Gotham City'
  },
  {
    category: 'Sport',
    question: 'How many players are on a soccer team?',
    choices: [
      '11', 
      '8',
      '10'
    ],
    answer: '11'
  }
];

const index = Math.floor(Math.random() * questions.length);
const choiceIndex = Math.floor(Math.random() * questions[index].choices.length);

function getRandomQuestion(arr) {
  return arr[index];

}

const result1 = getRandomQuestion(questions);
console.log(result1);

function getRandomComputerChoice(arr) {
  return arr[choiceIndex];
}

console.log('');

const result2 = getRandomComputerChoice(questions[index].choices);
console.log(result2);

function getResults(questionObj, computerChoice) {
  if (computerChoice === questionObj.answer) {
    return 'The computer\'s choice is correct!';

  }else if (computerChoice !== questionObj.answer) {
    return `The computer's choice is wrong. The correct answer is: ${questionObj.answer}`;
  }
}
console.log('');
const result3 = getResults(getRandomQuestion(questions), getRandomComputerChoice(questions[index].choices));

console.log(result3);

Hi. Can you please post the link to the url of the challenge you are doing.

Welcom :slight_smile:

What is your problem? I don’t see the text of your problem!

I already pass this test, i’m just wondering if there are improvements i can make for this code.

Welcome to the forum

With the questions array, you need to repeat the same property structure when adding an item. Later in the curriculum you will learn about classes, which can simplify this process.

With random numbers, you could potential receive the same question two times in a row. How about a function that makes sure that the same question is not asked twice?

Happy coding