mardi 30 juillet 2019

How can I select random N number of questions from array containing many questions?

I am trying to make a quiz with React that displays random 100 questions from a javascript file.

Here are the questions inside javascript file.

const quizQuestions = [
        
        {
                question: "Grand Central Terminal, Park Avenue, New York is the world's",
                options: ["largest railway station", "highest railway station", "longest railway station", "None of the above"],
                answer: "largest railway station"
        },
        {
                question: "Entomology is the science that studies",
                options: ["Behavior of human beings", "Insects", "The origin and history of technical and scientific terms", "The formation of rocks"],
                answer: "The origin and history of technical and scientific terms"
        },
        {
                question: "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of",
                options: ["Asia", "Africa", "Europe", "Australia"],
                answer: "Africa"
        },
        {
                question: "Garampani sanctuary is located at",
                options: ["Junagarh, Gujarat", "Diphu, Assam", "Kohima, Nagaland", "Gangtok, Sikkim"],
                answer: "Diphu, Assam"
        },
        {
                question: "Hitler party which came into power in 1933 is known as",
                options: ["Labour Party", "Nazi Party", "Ku-Klux-Klan","Democratic Party"],
                answer: "Nazi Party"
        }
        
]

export default quizQuestions;

1. I need javascript code to select for example 3 random questions in random order from above file. Similarly randomize options without repetition of question

2. And I want to execute the following function as many times as the no of questions by calling the component MCQ and passing the question and option as props

function MCQ(props) {

        return(
                <div>
                        <div>{props.question}</div>
                        <div> 
                                <input type="radio" name="answer" id=??? /><label for="???"> {props.options[0]}</label>
                                <input type="radio" name="answer" id=??? /><label for="???"> {props.options[1]}</label>
                                <input type="radio" name="answer" id=??? /><label for="???"> {props.options[2]}</label>
                                <input type="radio" name="answer" id=??? /><label for="???"> {props.options[3]}</label>
                        </div>
                </div>
                )
}

What id and name should i give?

THANK YOU




Aucun commentaire:

Enregistrer un commentaire