JSFiddle - React, Tailwind, and code Playground
by dife
HTML
<h1>Exemplo de QUIZ em JS</h1>
<div class="quiz-container">
<div id="quiz"></div>
</div>
<button id="previous">Questão anterior</button>
<button id="next">Próxima questão</button>
<button id="submit">Enviar Quiz</button>
<div id="total"></div>
<div id="results"></div>
CSS
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300,600);
body{
font-size: 20px;
font-family: 'Work Sans', sans-serif;
color: #333;
font-weight: 300;
text-align: center;
background-color: #ccc;
}
h1{
font-weight: 300;
margin: 0px;
padding: 10px;
font-size: 20px;
background-color: #444;
color: #fff;
}
.question{
font-size: 30px;
margin-bottom: 10px;
}
.answers {
margin-bottom: 20px;
text-align: left;
display: inline-block;
}
.answers label{
display: block;
margin-bottom: 10px;
}
button{
font-family: 'Work Sans', sans-serif;
font-size: 22px;
background-color: #279;
color: #fff;
border: 0px;
margin-top: 20px;
border-radius: 3px;
padding: 20px;
cursor: pointer;
margin-bottom: 20px;
}
button:hover{
background-color: #38a;
}
.slide{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
z-index: 1;
opacity: 0;
transition: opacity 0.5s;
}
.active-slide{
opacity: 1;
z-index: 2;
}
.quiz-container{
position: relative;
height: 200px;
margin-top: 40px;
}
Resources
JavaScript
(function() {
const myQuestions = [
{
question: "Qual o nome da substância que deixa o tomate vermelho?",
answers: {
a: "Ricopeno",
b: "Licopeno",
c: "Dicopeno",
d: "Sicopeno"
},
correctAnswer: "b"
},
{
question: "Quando foi a primeira aparição do Coringa?",
answers: {
a: "Março de 1970",
b: "Janeiro de 1939",
c: "Abril de 1940",
d: "Julho de 1950"
},
correctAnswer: "c"
},
{
question: "Na série Game of Thrones, qual é o nome da atriz da mãe dos dragões?",
answers: {
a: "Emilia Clarke",
b: "Carice Van Houten",
c: "Lena Headey",
d: "Gwendoline Christie"
},
correctAnswer: "a"
},
{
question: "O brasil faz fronteira com: ",
answers: {
a: "7 Países",
b: "5 Países",
c: "9 Países",
d: "10 Países"
},
correctAnswer: "d"
}
];
function buildQuiz() {
// we'll need a place to store the HTML output
const output = [];
// for each question...
myQuestions.forEach((currentQuestion, questionNumber) => {
// we'll want to store the list of answer choices
const answers = [];
// and for each available answer...
for (letter in currentQuestion.answers) {
// ...add an HTML radio button
answers.push(
`<label>
<input type="radio" name="question${questionNumber}" value="${letter}">
${letter} :
${currentQuestion.answers[letter]}
</label>`
);
}
// add this question and its answers to the output
output.push(
`<div class="slide">
<div class="question"> ${currentQuestion.question} </div>
<div class="answers"> ${answers.join("")} </div>
</div>`
);
});
// finally combine our output list into one string of HTML and put it on the page
...