Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I realized that the questions array is an object after using the typeof method.
I’ve tried writing the objects in side the array directly,but test 1-8 doesn’t pass on doing this.
how can I go about this?

Your code so far

const questions = []
  const object1 ={
    category: "Mathematics",
    question: "solve for x in : x^2 +8x + 6 = 0?",
    choices: ["-3 twice","2 and 3","-4 twice"],
    answer: "-4 twice"
  }
  const object2 ={
    category: "chemistry",
    question: "what gas law state : the sum of the partial pressure of a gas is equal to the pressure of the gas?",
    choices: ["avogadro","gay lussac","max carl"],
    answer: "gay lussac"
  }
const object3 ={
    category: "physics",
    question: "A machine carry a load of a 50N through a height of 20m in 10minutes. if the machine power is limited to 2Watts.find the efficiency of the machine?",
    choices: ["0.967","0.7889","0.833"],
    answer: "0.833"
  }
   const object4 ={
    category: "History",
    question: "how many are the ulul azm among the prophets of Allah?",
    choices: ["7","6","5"],
    answer: "5"
  }
   const object5 ={
    category: "fiqh",
    question: "which of the following vitiates the ablution of  the one that wants to offer salah?",
    choices: ["food","urination","talking"],
    answer: "urination"
  }

 questions.push(object1,object2,object3,object4,object5);
 function getRandomQuestion(questions){
    let randomNum = Math.round(Math.random()*questions.length)
    return questions[randomNum].question
 }
getRandomQuestion();

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

1 Like

Welcome to the forum @abdulbaari

The console is giving you a message.

TypeError: Cannot read properties of undefined (reading ‘length’)

Happy coding

Yes, but how do I go about it ?
Because I tried putting the objects in the array directly but all the tests from 1 to 8 is not passing.
So the .length property is not working for the array.

the issue is that you are trying to get the length of something that is undefined

when you call the function, what value are you passing in?

I’m trying to get the length of the questions array and it is defined at the top of the code.

The issue is that the array is showing object when I checked it’s type via the typeof method

you did not answer this question

find the line where you are calling the function, what value are you passing to the function?

There are no objects in your questions array.

it’s filled up later

Totally overlooked that…

I forgot to pass the question array as the argument
but the test as now passed , thank you for being there for me!

Tell us what’s happening:

test 9,11 and 13 didn’t pass despite getting the right output in the console.
how do I solve this ?

Your code so far

const questions = []
  const object1 ={
    category: "Mathematics",
    question: "solve for x in : x^2 +8x + 6 = 0?",
    choices: ["-3 twice","2 and 3","-4 twice"],
    answer: "-4 twice"
  }
  const object2 ={
    category: "chemistry",
    question: "what gas law state : the sum of the partial pressure of a gas is equal to the pressure of the gas?",
    choices: ["avogadro","gay lussac","max carl"],
    answer: "gay lussac"
  }
const object3 ={
    category: "physics",
    question: "A machine carry a load of a 50N through a height of 20m in 10minutes. if the machine power is limited to 2Watts.find the efficiency of the machine?",
    choices: ["0.967","0.7889","0.833"],
    answer: "0.833"
  }
   const object4 ={
    category: "History",
    question: "how many are the ulul azm among the prophets of Allah?",
    choices: ["7","6","5"],
    answer: "5"
  }
   const object5 ={
    category: "fiqh",
    question: "which of the following vitiates the ablution of  the one that wants to offer salah?",
    choices: ["food","urination","talking"],
    answer: "urination"
  }

 questions.push(object1,object2,object3,object4,object5);
 let randomNum = Math.round(Math.random()*(questions.length-1));
 function getRandomQuestion(questions){
    return questions[randomNum]
 }
getRandomQuestion(questions);
 let options = questions[randomNum].choices
 let randomAnswer = questions[randomNum].answer
 let choice = []
function getRandomComputerChoice(options){
  return randomAnswer
}
 getRandomComputerChoice(options)
 
 let computerChoice = getRandomComputerChoice(options)
 function getResults(object
 , computerChoice){
   let rightAnswer = questions[randomNum].answer
  if(computerChoice === rightAnswer){
    return "The computer's choice is correct!"
  }
  if(computerChoice !== rightAnswer){
    return `The computer's choice is wrong. The correct answer is: ${rightAnswer}`
 }
 }
  getResults(questions[randomNum],computerChoice)
console.log(getRandomQuestion(questions))
console.log(getRandomComputerChoice(options))
console.log(getResults(questions[randomNum],computerChoice))

Your browser information:

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

Challenge Information:

Build a Quiz Game - Build a Quiz Game

Review the tests and the relevant user stories to make sure everything is implemented as it should be.

Welcome to the forum @abdulbaari

The faded parameter means you are not using the value passed to the function.

Happy coding

I have merged your two topics, please do not create multiple topics for the same lab

I have tried sorting the getRandomComputerChoice function out , but the test is still not going.
here is My code

Please post your actual code instead of a picture. Thanks.

Did you see this reply?

You can see randomChoice is faded out because it’s not referenced at all within the function. Isn’t it odd for a function not to use the parameters it’s defined to use?

1 Like