#Setting Correct Answer in Quiz Array

47 messages · Page 1 of 1 (latest)

unborn canopyBOT
#

hi guys, I will build an array of quizzes.

just trying to think, how would would you set one of the radio options as "true" or "correct"

https://payloadcms.com/docs/fields/radio#example

only thing I can think of is using a Number field with min 1, max 4. and they have to put a number in when creating the quizzes.

unless the radio field has something built in?

  questions: [
    {
      id: 1,
      question: "What is the main topic of this chapter?",
      options: ["Science", "Religion", "History", "Maths"],
      correctAnswer: 3
    },
  ]
}```

Is my approach fine?
Payload

Payload is a headless CMS and application framework built with TypeScript, Node.js, React and MongoDB

leaden copperBOT
#

Original message from @balmy fable - Moved from #general message

#
limpid sail
#

Hey @balmy fable,

I can help ya here

#

So, this is on your frontend correct?

balmy fable
#

yes

limpid sail
#

In payload you have a collection with a radio field

#

Or an array of fields with radio fields on them per quiz

balmy fable
#

yes I am looking at radio field on payload, i just dont see how you can set one of the options to "true"

limpid sail
#

Ahh, I see, it sounds like you need defaultValue

balmy fable
#

wouldn't defaultValue give away the answer?

limpid sail
#

I see what you're asking for, this isn't really unique to payload, but what you would do is store your users selection in state

#

Then when ready, update your field in payload via REST api

balmy fable
#

I will avoid using the database, and do it all locally/useState

#

I'm just getting the quiz data from payload, then its all client side

limpid sail
#

So, the user selects option A, then the value of your field would be that same option that is defined in your config

balmy fable
#

but the issue, it will be an array of quizzes

limpid sail
#

That's fine, you just do the same for all

#

But if you're not planning to use Payload to actually save the answers then this is kind of off-topic

balmy fable
#

yeah I want to get the data from payload

#

saving is much down the line, this is MVP

limpid sail
#

I'm not seeing a question here to be honest... do you want to walk me through what you're currently stuck on?

balmy fable
#

let me just start again

#

maybe I have a temp solution already

#

I will have a collection called books.

Each book will have an array of quizzes

const quiz = {
  questions: [
    {
      id: 1,
      question: "What is the main topic of this chapter?",
      options: ["Science", "Religion", "History", "Maths"],
      correctAnswer: 3
    },
 // more quizzes here
  ]
}

I just want to know if the radio field has the ability to set true to one of the options via the CMS?
if not I will use a numbers field called "correctAnswer"

all validation is happening client side for now

#

(since it's MVP)

limpid sail
#

AHHH Yes, this makes much more sense

#

What I would honestly do, is create a new field with type Text

#

And put the answer in there

#

And then it's just a simple equality check

#

Yeah, your intuition was right

balmy fable
#

ah okay, so its just relying on whoever populates the quizzes to make sure they matched it up haha

#

fair enough, makes sense

#

thanks for your help, I think you've helped me solve this

limpid sail
#

It's my pleasure! And yeah, your inital approach would absolutely work

#

Let me know when you're ready to store those answers!

#

If you ever even need to

balmy fable
#

only thing smarter I could imagine if the Select field could grab/mirror the fields from the radio groups ?

#

if not we've solved this

#

overall I think we're good to store these answers ^

limpid sail
#

You could do this

#

I would move your options out into it's own const and then just use the same variable for both options

#

In your select and radio

#

And in select, you could mark defaultValue to the correct option, and then set it to readOnly since I'm assuming the correct answer will never change

balmy fable
#

sounds like a good suggestion

#

I will give it a try, i'm happy with the results.

#

many thanks for your help