Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

не проходят тесты 8-9 и 11-14, хотя в консоли всё выводит и программа работает

Your code so far

const questions = [
  {
    category: 'FF',
    question: 'fav food?',
    choices: ['burger', 'lolipop', 'cake'],
    answer: 'cake'
  },
  {
    category: 'FB',
    question: 'fav brand?',
    choices: ['Gucci', 'Versace', 'Adidas'],
    answer: 'Versace'
  },
  {
    category: 'FS',
    question: 'fav school subject?',
    choices: ['Math', 'Computer Technologies', 'Biology'],
    answer: 'Math'
  },
  {
    category: 'FW',
    question: 'fav weather?',
    choices: ['sunny', 'rainy', 'another'],
    answer: 'sunny'
  },
  {
    category: 'FM',
    question: 'fav month?',
    choices: ['April', 'October', 'February'],
    answer: 'April'
  }
];
const randomNum = Math.round(Math.random() * questions.length);
const anotherRandomNum = Math.round(Math.random() * 3);

function getRandomQuestion(questions) {
  return questions[randomNum].question;
}

function getRandomComputerChoice(questions) {
  return questions[randomNum].choices[anotherRandomNum];
}
const question = getRandomQuestion(questions);
const computerAnswer = getRandomComputerChoice(questions);
console.log(question);
console.log(computerAnswer); 

function getResults(question, computerAnswer){
  if (computerAnswer === questions.answer) {
    return "The computer's choice is correct!"
  } else {
    return `The computer's choice is wrong. The correct answer is: ${questions.answer}`
  }
} 
console.log(getResults()); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-quiz-game/66f17db06803d11a1bd19a20.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @pepel.xaxa,

This code should not be in the global scope.

Using Math.round on Math.random won’t produce the desired result, and you should not be hard coding the length of the choices array…what if it changes?

Have a look at this reference to learn how you can get a number between 0 (inclusive) and the length of the array (exclusive): JavaScript Math random() Method

What does User Story #7 say getRandomQuestion should return? Is that what you are returning?

What does User Story #8 say you should pass to getRandomComputerChoice as a parameter? Is that what you’re passing in?

Start by fixing these issues then post your updated code, formatted as follows, if you need more help:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Happy coding

const questions = [
  {
    category: 'FF',
    question: 'fav food?',
    choices: ['burger', 'lolipop', 'cake'],
    answer: 'cake'
  },
  {
    category: 'FB',
    question: 'fav brand?',
    choices: ['Gucci', 'Versace', 'Adidas'],
    answer: 'Versace'
  },
  {
    category: 'FS',
    question: 'fav school subject?',
    choices: ['Math', 'Computer Technologies', 'Biology'],
    answer: 'Math' 
  },
  {
    category: 'FW',
    question: 'fav weather?',
    choices: ['sunny', 'rainy', 'another'],
    answer: 'sunny'
  },
  {
    category: 'FM',
    question: 'fav month?',
    choices: ['April', 'October', 'February'],
    answer: 'April'
  }
];

function getRandomQuestion(questions) {
  const randomIndex = Math.floor(Math.random() * questions.length);
  return questions[randomIndex];
}

function getRandomComputerChoice(choices) {
  const randomIndex = Math.floor(Math.random() * choices.length);
  return choices[randomIndex];
}

const randomQuestionObj = getRandomQuestion(questions);
const computerChoice = getRandomComputerChoice(randomQuestionObj.choices);

console.log(randomQuestionObj.question);
console.log(computerChoice);

function getResults(randomQuestionObj, computerChoice) {
  if (computerChoice === randomQuestionObj.answer) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${randomQuestionObj.answer}.`;
  }
}

const result = getResults(randomQuestionObj, computerChoice);
console.log(result);

Вот мой отредактированный код, однако 11, 13 и 14 тесты не проходит

Good job!

Now you just need to remove the hard stop/period after the answer.