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
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.
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
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?