I'm working on a simple quiz application. I have a local array with questions that is in randomised order each time. The function that is shuffling the array in different order is triggered each time the handleAnswerOptionClick is run.
How can I run the arrayShuffle function once?
My guess is that I could use useEffect for this but I haven't gotten it to work. It complains about that it doesn't have different dependencies.
useEffect(() => {
arrayShuffle(questions);
}, []);
Code: CodeSandox
QuizContainer:
import React, { useState, useEffect } from "react";
export const QuizContainer = () => {
const questions = [
{
questionText: "What is the capital of France?",
answerOptions: [
{ option: "New York", isCorrect: false },
{ option: "London", isCorrect: false },
{ option: "Paris", isCorrect: true },
{ option: "Dublin", isCorrect: false }
]
},
{
questionText: "Who is CEO of Tesla?",
answerOptions: [
{ option: "Jeff Bezos", isCorrect: false },
{ option: "Elon Musk", isCorrect: true },
{ option: "Bill Gates", isCorrect: false },
{ option: "Tony Stark", isCorrect: false }
]
},
{
questionText: "The iPhone was created by which company?",
answerOptions: [
{ option: "Apple", isCorrect: true },
{ option: "Intel", isCorrect: false },
{ option: "Amazon", isCorrect: false },
{ option: "Microsoft", isCorrect: false }
]
},
{
questionText: "How many Harry Potter books are there?",
answerOptions: [
{ option: "1", isCorrect: false },
{ option: "4", isCorrect: false },
{ option: "6", isCorrect: false },
{ option: "7", isCorrect: true }
]
}
];
const [currentQuestion, setCurrentQuestion] = useState(0);
const [showScore, setShowScore] = useState(false);
const [score, setScore] = useState(0);
// Event handlers
const handleAnswerOptionClick = (isCorrect) => {
if (isCorrect) {
setScore(score + 1);
}
const nextQuestion = currentQuestion + 1;
nextQuestion < questions.length
? setCurrentQuestion(nextQuestion)
: setShowScore(true);
};
// ShuffleArray
const arrayShuffle = function (arr) {
let newPos, temp;
for (let i = arr.length - 1; i > 0; i--) {
newPos = Math.floor(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[newPos];
arr[newPos] = temp;
}
return arr;
};
const newArray = arrayShuffle(questions);
console.log(newArray);
return (
<>
<h1>QuizContainer</h1>
{showScore ? (
<p>
You scored {score} out of {questions.length}
</p>
) : (
<>
<p>
Question {currentQuestion + 1}/{questions.length}
</p>
<p>{questions[currentQuestion].questionText}</p>
<div>
{questions[currentQuestion].answerOptions.map(
(answerOption, index) => (
<div key={index}>
<button
onClick={() =>
handleAnswerOptionClick(answerOption.isCorrect)
}
>
{answerOption.option}
</button>
</div>
)
)}
</div>
</>
)}
</>
);
};
Just ask if I need to clarify and if you guys think that I'm approaching this the right way.
Thanks beforehand,
Erik