When I click the button my website stucks I dont know what is happening. I am using the while loop to not repeat the question in my quiz app. Help will be appreciated. The App and Questions function components are listed below. I am bringing my questions from an object array.
App.js
'''
import "./App.css";
import Questions from "./layout/Questions";
function App() {
return (
<div>
<Questions />
</div>
);
}
export default App;
'''
Questions.js
import React, { useState } from "react";
import mcqs from "../data/mcqs";
const Questions = () => {
const handleButtonClick = () => {
setquestionCounter(questionCounter + 1);
};
let randomQuestion = Math.floor(Math.random() * mcqs.length);
let [questionCounter, setquestionCounter] = useState(1);
if (questionCounter <= mcqs.length) {
while (mcqs[randomQuestion].question === "999") {
randomQuestion = Math.floor(Math.random() * mcqs.length);
}
}
const string = mcqs[randomQuestion].question;
mcqs[randomQuestion].question = "999";
return (
questionCounter <= mcqs.length && (
<div className="questions">
Q{questionCounter}:{string}
<button onClick={handleButtonClick}>Next Question</button>
</div>
)
);
};
export default Questions;
question from:
https://stackoverflow.com/questions/65899941/how-do-i-show-random-question-without-repetition 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…