Hice una tarjeta flash que mostrará las preguntas que guardé en la matriz y luego haga clic para obtener otras preguntas. Pero tengo un problema si en el medio hago clic en el botón de reinicio. por ejemplo, tengo 6 preguntas y hago clic en reiniciar cuando el progreso es 3, el progreso se restablecerá a 1 y ejecutará la pregunta del 1 al 6, pero en mi código continúa 4 5 6.
var btnRestart=document.getElementById("restart"); var btnNext=document.getElementById("next"); var words = [{ question: "HTML stands for", answer: "HyperText Markup Language" }, { question: "What is a href?", answer: "Link location attribute" }, { question: "What is extensions used to save HTML pages?", answer: ".html" }, { question: "What type of video formats are supported by HTML5", answer: "MP4, WebM, Ogg" }, { question: "What is _blank target attribute?", answer: "It is opens the document in a new window or tab" }, { question: "How to handle events in HTML?", answer: "Using javaScript or jQuery." }, { question: "What is _parent target attribute?", answer: "It is opens the document in a parent frame" } ]; randomQuestions(); var count = 1; function randomQuestions() { count++; var tempWords = []; for (var i = 0; i < words.length; i++) { tempWords.push(i); } //last question if (tempWords.length === 1) { btnNext.style.display = 'none'; } let index = Math.floor(Math.random() * tempWords.length); const question = words.splice(index, 1)[0] document.getElementById("question").innerHTML = question.question; } btnNext.addEventListener('click', function(){ randomQuestions(); }); btnRestart.addEventListener('click', function() { randomQuestions(); tempWords = []; count = 1; })HTML
<div id="question"> <h4>Questions</h4> </div> <button id="next">Next</button> <button id="restart">Restart</button>Compruebe si esto puede funcionar para usted:
var btnRestart = document.getElementById("restart"); var btnNext = document.getElementById("next"); const questionElem = document.getElementById("question") var count = 0; var words = [{ question: "HTML stands for", answer: "HyperText Markup Language" }, { question: "What is a href?", answer: "Link location attribute" }, { question: "What is extensions used to save HTML pages?", answer: ".html" }, { question: "What type of video formats are supported by HTML5", answer: "MP4, WebM, Ogg" }, { question: "What is _blank target attribute?", answer: "It is opens the document in a new window or tab" }, { question: "How to handle events in HTML?", answer: "Using javaScript or jQuery." }, { question: "What is _parent target attribute?", answer: "It is opens the document in a parent frame" } ]; let leftWords = [...words] randomQuestions(); function randomQuestions() { if (count === words.length - 1) btnNext.style.display = 'none'; const idx = Math.round(Math.random() * (leftWords.length - 1)) console.log("RANDOM IDX", idx, "QUESTIONS LEFT:", leftWords.length) questionElem.innerHTML = leftWords[idx].question; leftWords.splice(idx, 1) count++; } btnNext.addEventListener('click', randomQuestions); btnRestart.addEventListener('click', function() { btnNext.style.display = 'inline'; count = 0 leftWords = [...words] randomQuestions(); }) <div id="question"> <h4>Questions</h4> </div> <button id="next">Next</button> <button id="restart">Restart</button>