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">
</head>
<body>
<div class="content">
<div class="content-quiz">
<div class="content-quiz-questions">
<h2 id="header-questions"></h2>
</div>
<div class="content-quiz-answers">
<ul>
<li>
<input type="radio" id="answer_a" name="answer" class="answer">
<label id="a" for="answer_a"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="answer_b" name="answer" class="answer">
<label id="b" for="answer_b"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="answer_c" name="answer" class="answer">
<label id="c" for="answer_c"></label>
</li>
</ul>
<ul>
<li>
<input type="radio" id="answer_d" name="answer" class="answer">
<label id="d" for="answer_d"></label>
</li>
</ul>
</div>
</div>
<button type="submit" class="btn-submit">Submit</button>
</div>
</body>
</html>
JavaScript
let oldQuestionIndex = -1;
const headerQuestions = document.getElementById('header-questions');
const answerA = document.getElementById('a');
const answerB = document.getElementById('b');
const answerC = document.getElementById('c');
const answerD = document.getElementById('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 Lamborginis'
]
},
{
question: 'What year was JavaScript launched?',
answers: ['1996', '1995', '1994', 'none of the above']
}
];
function showRandomQuestions() {
do {
var randomQuestIndex = Math.floor(Math.random() * quizArr.length);
} while (randomQuestIndex === oldQuestionIndex);
oldQuestionIndex = randomQuestIndex;
headerQuestions.innerHTML = quizArr[randomQuestIndex].question;
}
btnSubmit.addEventListener('click', function() {
showRandomQuestions();
});
showRandomQuestions();