the parameter should be array of choices and the return should be random choice as the user story says
i donno why i feel so bad about my learning curve nowadays.
i thought i was much better than that ![]()
Yes, the input is an array of options and the output is a random choice from that array.
So now you can look at the two questions
-
how do you get a random choice from the input array
-
how do you pass the correct array to the function when calling it
Programming is all about knowing what you want the computer to do for you.
should it depend on the pre function that returns a random question ? then i pick the array of choices from it and return a random choice ?
What is the ‘pre function’ you are thinking about? Can you fill in more details of what you’re thinking?
the getRandomQuestion function
so that i get choices relevant with the choosen question ?
Well, what does that function return? Does it return exactly the input for the getRandomComputerChoice function?
ok now i executed it as i explained to u and i got the check mark on the test i was fail in i put the function param
wait i send you my updated code
const questions = [
{
category: 'sports',
question: 'Who is the greatest footballer in africa ?',
choices: ['M.Abo trika', 'H.Shehata', 'M.Elkhateeb'],
answer: 'M.Abo trika',
},
{
category: 'sports',
question: 'Who is the greatest footballer in Europe ?',
choices: ['Messi', 'C.Ronaldo', 'Ibrahimovic'],
answer: 'Messi',
},
{
category: 'sports',
question: 'Who is the greatest footballer in Latin America ?',
choices: ['Batistuta', 'Ronaldo', 'Romario'],
answer: 'Romario',
},
{
category: 'sports',
question: 'Who is the greatest footballer in America ?',
choices: ['Donovan', 'Kawalenko', 'Corrales'],
answer: 'Donovan',
},
{
category: 'policy',
question: 'Who is the greatest politican in Egypt ?',
choices: ['Naguib', 'Sadat', 'Mubarak'],
answer: 'Mubarak',
},
]
const getRandomQuestion = (arrOfQuestions) => {
return arrOfQuestions[Math.floor(Math.random() * arrOfQuestions.length)]
}
// console.log(getRandomQuestion(questions).choices)
const getRandomComputerChoice = (avChoices) => {
let random5 = Math.floor(Math.random() * avChoices.length)
let random3 = Math.floor(Math.random() * 3)
return avChoices[random3]
}
const getResults = (question, comChoice) => {
return comChoice
}
console.log(getRandomComputerChoice(getRandomQuestion(questions).choices))
// console.log(getResults(getRandomQuestion(questions), getRandomComputerChoice(questions.choices)))
Did you complete this successfully or do you still need help?
jeremey helped me much with it till i passed it succefully,
now i will move to the next part i see , i feel there is a trouble there
ok now iam on the getResults function
but there is a typeerror message on the console
TypeError: Cannot read properties of undefined (reading 'length')
Ok, what line is that error happening on, and why might something in that line be ‘undefined’?
um how can i find it i should go to developer tools to be sure or what ?
The console should tell you which line the error occured on
this is the full message
TypeError: Cannot read properties of undefined (reading ‘length’)
There should be more above that line in the console
can i capture my screen and send it to you or there’s something wrong in my browser
Open your browser’s console to see the line that the error is on. The line number doesn’t show up in the fCC console.
javascript-test-evaluator.js:2 TypeError: Cannot read properties of undefined (reading ‘length’)
at getRandomComputerChoice (eval at #i (javascript-test-evaluator.js:2:138025), :53:54)
at eval (eval at #i (javascript-test-evaluator.js:2:138025), :71:54)
at #i (javascript-test-evaluator.js:2:138025)
at JavascriptTestEvaluator.handleMessage (javascript-test-evaluator.js:2:138696)
at onmessage (javascript-test-evaluator.js:2:138986)