Hello everyone, I am working on creating a Quiz app in React Native that displays questions randomly without any duplication. So far, I have managed to display the Quiz questions randomly, but I'm stuck on preventing duplicates. This is a new learning experience for me with React Native and I would appreciate any help or ideas on how to solve this issue. I am using a Macbook Pro, Visual Code Studio, and React Native.
Below is the code snippet:
export function App() {
const questions = [
{
questionText: "city",
answerOptions: [
{ answerText: "canada", isCorrect: false },
{ answerText: "USA", isCorrect: false },
{ answerText: "Napoli", isCorrect: true },
{ answerText: "Brazil", isCorrect: false },
],
},
{
questionText: "country",
answerOptions: [
{ answerText: "Paris", isCorrect: false },
{ answerText: "London", isCorrect: false },
{ answerText: "Spain", isCorrect: true },
{ answerText: "Rome", isCorrect: false },
],
},
...
];
const randomQuestions =
questions[Math.floor(Math.random() * questions.length)];
return (
<View style={styles.container}>
<Text style={styles.question}>
{randomQuestions.questionText}
</Text>
<Text style={styles.answer}>
{randomQuestions.answerOptions[0].answerText}
</Text>
<Text style={styles.answer}>
{randomQuestions.answerOptions[1].answerText}
</Text>
<Text style={styles.answer}>
{randomQuestions.answerOptions[2].answerText}
</Text>
<Text style={styles.answer}>
{randomQuestions.answerOptions[3].answerText}
</Text>
</View>
);
};