JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Aplikacja quizu</title>
<meta name="keywords" content="aplikacja, quiz, pytania, odpowiedzi">
<meta name="description" content="Podstawowa aplikacja do quizu">
<link rel="stylesheet" href="style.css">
<script src="index.js" defer></script>
</head>
<body>
<div class="content">
<div class="content-quiz">
<div class="content-quiz-questions">
<h2 id="header-question"></h2>
</div>
<div class="content-quiz-answers">
<ul>
<li>
<input type="radio" id="a" name="answer" class="answer">
<label id="label-answer_A" for="a"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="b" name="answer" class="answer">
<label id="label-answer_B" for="b"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="c" name="answer" class="answer">
<label id="label-answer_C" for="c"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="d" name="answer" class="answer">
<label id="label-answer_D" for="d"></label>
</li>
</ul>
</div>
</div>
<button type="submit" class="btn-submit">Submit</button>
</div>
</body>
</html>
JavaScript
const headerQuestion = document.getElementById('header-question');
let oldQuestionIndex = -1;
const labelAnswerA = document.getElementById('label-answer_A');
const labelAnswerB = document.getElementById('label-answer_B');
const labelAnswerC = document.getElementById('label-answer_C');
const labelAnswerD = document.getElementById('label-answer_D');
let arrAnswerVaranty = ['a', 'b', 'c', 'd'];
const btnSubmit = document.querySelector('.btn-submit');
const quizArr = [{
question: 'What is the most used programming language in 2019?',
answers: ['Java', 'C', 'Python', 'JS']
},
{
question: 'Who is the President of US?',
answers: ['Florin Pop', 'Joe Biden', 'Ivan Saldano', 'Mihai Andrei']
},
{
question: 'What does HTML stand for?',
answers: [
'Hypertext Markup Language',
'Cascading Style Sheet',
'Jason Object Notation',
'Helicopters Terminals Motorboats Lamborgini'
]
},
{
question: 'What year was JavaScript launched?',
answers: ['1996', '1995', '1994', 'none of the above']
}
];
function getRandomQuestion() {
let currentRandomIndex;
do {
currentRandomIndex = Math.floor(Math.random() * quizArr.length);
} while (currentRandomIndex === oldQuestionIndex);
oldQuestionIndex = currentRandomIndex;
return currentRandomIndex;
}
function getRandomVariants(questionIndex) {
const answersIndices = [];
for (const value of quizArr[questionIndex].answers) {
let oldAnswersIndex = -1;
do {
oldAnswersIndex = Math.floor(Math.random() * quizArr[questionIndex].answers.length);
} while (answersIndices.includes(oldAnswersIndex));
answersIndices.push(oldAnswersIndex);
}
return answersIndicies;
}
function showRandomQuestion() {
const displayQuestQiz = getRandomQuestion();
headerQuestion.innerHTML = displayQuestQiz;
}
btnSubmit.addEventListener('click', function() {
showRandomQuestion();
});
showRandomQuestion();