Skip to content

Instantly share code, notes, and snippets.

@harunpehlivan
Created June 6, 2021 22:11
Show Gist options
  • Save harunpehlivan/c744570abcac90b998854b7c869023c2 to your computer and use it in GitHub Desktop.
Save harunpehlivan/c744570abcac90b998854b7c869023c2 to your computer and use it in GitHub Desktop.
Quiz App #30days30submits #day-27
<section>
<div class="container">
<div class="scoreBoard"><span class="score-num">0</span>/<span class="answered-num">0</span></div>
<form id="quiz_form">
<h1>Q: <span class="qus">This is a QusThis is a Qus?</span></h1>
<div class="all_options">
</div>
<div class="buttons">
<button type="submit">Submit</button>
</div>
</form>
</div>
</section>
<!-- ******************* -->
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<!-- This Code is for only the floating card in right bottom corner -->
<!-- ******************** -->
<div id="webCifar-sidebox">
<div id="webCifar">
<h2 class="logo">HP IT GROUP (TEBIM TEBITAGEM) TTGRT</h2>
<p class="author">Coded By <span>HARUN PEHLİVAN </span> </p>
<div class="items">
<a href="https://www.youtube.com/user/harunpehlivan1" target="_blank" class="item youtubeLink">
<svg title="watch how we made this" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path d="M10 12a2 2 0 100-4 2 2 0 000 4z" />
<path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd" />
</svg>
<p>Watch how we made this.
</p>
</a>
<a href="https://harunpehlivanitcoderdesignerfounderceo.glitch.me/" target="_blank" class="item">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9" />
</svg>
<p>CODER,DESIGNER</p>
</a>
</div>
<div class="close">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</div>
</div>
<div id="webCifar-icon">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<p>Info About the pen</p>
</div>
</div>
(function () {
const baseURL = "https://opentdb.com/api.php?amount=1";
const containerEl = document.querySelector(".container");
const form = document.querySelector("#quiz_form");
const qusEl = document.querySelector(".qus");
const optionsEl = document.querySelector(".all_options");
const buttonsEl = document.querySelector(".buttons");
const scoreEl = document.querySelector(".scoreBoard .score-num"); /* */
const answeredEl = document.querySelector(".scoreBoard .answered-num"); /* */
let question, answer;
let options = [];
let score = 0;
let answeredQus = 0;
window.addEventListener("DOMContentLoaded", () => {
quizApp();
});
async function quizApp() {
updateScoreBoard();
addPlaceholder();
const data = await fetchQuiz();
question = data[0].question;
options = [];
answer = data[0].correct_answer;
data[0].incorrect_answers.map((item) => options.push(item));
options.splice(Math.floor(Math.random() * options.length + 1), 0, answer);
// console.log(answer)
generateTemplate(question, options, answer);
}
form.addEventListener("submit", (e) => {
e.preventDefault();
if (e.target.quiz.value) {
checkQuiz(e.target.quiz.value);
e.target.querySelector("button").style.display = "none";
generateButtons();
} else {
return;
}
});
async function fetchQuiz() {
const response = await fetch(baseURL);
const data = await response.json();
// console.log(data.results)
return data.results;
}
function generateTemplate(question, options, answer) {
removePlaceHolder();
optionsEl.innerHTML = "";
qusEl.innerText = question;
options.map((option, index) => {
const item = document.createElement("div");
item.classList.add("option");
item.innerHTML = `
<input type="radio" id="option${index + 1}" value="${option}" name="quiz">
<label for="option${index + 1}">${option}</label>
`;
optionsEl.appendChild(item);
});
}
function checkQuiz(selected) {
// console.log(selected, answer)
answeredQus++;
if (selected === answer) {
score++;
updateScoreBoard();
form.quiz.forEach((input) => {
if (input.value === answer) {
input.parentElement.classList.add("correct");
// console.log(input)
}
});
} else {
updateScoreBoard();
form.quiz.forEach((input) => {
if (input.value === answer) {
input.parentElement.classList.add("correct");
// console.log(input)
}
});
}
}
function generateButtons() {
const finishBtn = document.createElement("button");
finishBtn.innerText = "Finish";
finishBtn.setAttribute("type", "button");
finishBtn.classList.add("finish-btn");
buttonsEl.appendChild(finishBtn);
const nextBtn = document.createElement("button");
nextBtn.innerText = "Next Qus";
nextBtn.setAttribute("type", "button");
nextBtn.classList.add("next-btn");
buttonsEl.appendChild(nextBtn);
nextBtn.addEventListener("click", getNextQuiz);
finishBtn.addEventListener("click", finishQuiz);
}
function getNextQuiz() {
const nextBtn = document.querySelector(".next-btn");
const finishBtn = document.querySelector(".finish-btn");
buttonsEl.removeChild(nextBtn);
buttonsEl.removeChild(finishBtn);
buttonsEl.querySelector('button[type="submit"]').style.display = "block";
quizApp();
}
function finishQuiz() {
const nextBtn = document.querySelector(".next-btn");
const finishBtn = document.querySelector(".finish-btn");
buttonsEl.removeChild(nextBtn);
buttonsEl.removeChild(finishBtn);
buttonsEl.querySelector('button[type="submit"]').style.display = "block";
const overlay = document.createElement("div");
overlay.classList.add("result-overlay");
overlay.innerHTML = `
<div class="final-result">${score}/${answeredQus}</div>
<button>Play Again</button>
`;
containerEl.appendChild(overlay);
document
.querySelector(".result-overlay button")
.addEventListener("click", () => {
containerEl.removeChild(overlay);
playAgain();
});
console.log(score);
}
function updateScoreBoard() {
scoreEl.innerText = score;
answeredEl.innerText = answeredQus;
}
function playAgain() {
score = 0;
answeredQus = 0;
quizApp();
}
function addPlaceholder() {
const placeholder = document.createElement("div");
placeholder.classList.add("placeholder");
containerEl.appendChild(placeholder);
}
function removePlaceHolder() {
const placeholderEl = document.querySelector(".container .placeholder");
containerEl.removeChild(placeholderEl);
}
})();
// *********************
// This Code is for only the floating card in right bottom corner
// **********************
const WebCifarIcon = document.querySelector("#webCifar-icon");
const WebCifarEl = document.querySelector("#webCifar");
const close = WebCifarEl.querySelector(".close");
const youtubeLink = document.querySelector(".youtubeLink");
WebCifarIcon.addEventListener("click", () => {
WebCifarEl.classList.add("active");
});
close.addEventListener("click", () => {
WebCifarEl.classList.remove("active");
});
youtubeLink.setAttribute("href", "https://www.youtube.com/user/harunpehlivan1");
@import url("https://fonts.googleapis.com/css2?family=Nunito:wght@600&display=swap");
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html {
font-family: "Nunito", sans-serif;
font-size: 20px;
}
section {
min-height: 100vh;
width: 100%;
background-color: rgb(25, 25, 26);
display: flex;
align-items: center;
justify-content: center;
}
.container {
background-color: rgb(40, 40, 44);
padding: 50px 30px 30px 30px;
width: 90%;
max-width: 600px;
min-height: 500px;
border-radius: 8px;
color: aliceblue;
position: relative;
overflow: hidden;
}
.container .scoreBoard {
position: absolute;
top: 20px;
right: 20px;
}
form h1 {
font-size: 1.4rem;
line-height: 2rem;
margin-bottom: 2rem;
}
form .option {
font-size: 1.2rem;
margin: 10px 0 0 10px;
padding: 10px;
transition: 0.3s ease background-color;
border-radius: 4px;
width: 100%;
display: flex;
align-items: center;
justify-content: stretch;
}
form .option label {
width: 100%;
margin-left: 10px;
cursor: pointer;
justify-self: stretch;
}
form .option:hover {
background-color: rgb(23, 23, 24);
}
form .option input[type="radio"]:checked + label {
color: rgb(0, 238, 255);
}
form .option.correct {
background-color: green;
}
form button,
.container .result-overlay button {
width: 100%;
margin-top: 20px;
padding: 10px;
font-size: 1.3rem;
font-family: "Montserrat";
background-color: rgb(98, 100, 105);
border: none;
outline: none;
color: rgb(20, 20, 20);
cursor: pointer;
transition: 0.3s ease background-color;
}
button:hover,
.container .result-overlay button:hover {
background-color: rgb(145, 148, 155);
}
.finish-btn,
.next-btn {
width: 50%;
display: inline-block;
}
.finish-btn {
border-right: 1px solid black;
}
.next-btn {
border-left: 1px solid black;
}
.container .result-overlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: rgb(40, 40, 44);
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.container .result-overlay .final-result {
font-size: 5rem;
}
.container .placeholder {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-image: url(https://res.cloudinary.com/tercuman-b-l-m-merkez/image/upload/v1602655723/lKcFz4y_xh3o0w.jpg);
background-color: rgb(40, 40, 44);
background-position: center;
background-size: 90%;
background-repeat: no-repeat;
}
@media (max-width: 768px) {
html {
font-size: 17px;
}
.container form h1 {
font-size: 1.5rem;
}
}
/* ********************* */
/* This Code is for only the floating card in right bottom corner */
/* ********************** */
@import url("https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap");
* {
padding: 0;
margin: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
a {
padding: 0;
margin: 0;
color: var(--color-4);
text-decoration: none;
}
#webCifar-sidebox {
position: absolute;
right: 0px;
bottom: 0px;
overflow-x: clip;
width: 300px;
font-size: 16px;
}
#webCifar-sidebox p {
padding: 0;
margin: 0;
}
#webCifar {
--color-1: #17bcb4;
--color-2: #24252a;
--color-3: #244044;
--color-4: #f3f8f7;
background: var(--color-2);
display: inline-block;
color: var(--color-4);
padding: 10px 17px;
border-radius: 6px;
font-family: "Roboto Mono", monospace;
text-align: center;
position: absolute;
right: 5px;
bottom: 5px;
-webkit-transform: translateX(calc(100% + 5px));
transform: translateX(calc(100% + 5px));
-webkit-transition: 0.5s ease-out transform;
transition: 0.5s ease-out transform;
z-index: 4;
}
#webCifar.active {
-webkit-transform: translateX(0);
transform: translateX(0);
}
#webCifar .logo {
font-size: 25px;
}
#webCifar .author {
margin-top: 10px;
margin-bottom: 20px;
}
#webCifar .author span {
background-color: var(--color-3);
padding: 3px;
border-radius: 4px;
}
#webCifar .items {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: start;
-ms-flex-align: start;
align-items: start;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
#webCifar .item {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
gap: 10px;
padding: 5px;
border-radius: 4px;
text-align: left;
}
#webCifar .item:hover {
background-color: var(--color-3);
}
#webCifar svg {
max-width: 20px;
}
#webCifar .close {
position: absolute;
display: inline-block;
height: 30px;
width: 30px;
right: 5px;
top: 5px;
padding: 5px;
background-color: var(--color-3);
border-radius: 50%;
font-size: 20px;
cursor: pointer;
}
#webCifar-icon {
--color-2: #24252a;
--color-3: #244044;
font-family: "Roboto Mono", monospace;
text-align: left;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
background-color: var(--color-2);
color: white;
width: -webkit-fit-content;
width: -moz-fit-content;
width: fit-content;
padding: 5px;
border-radius: 4px;
gap: 5px;
margin: 5px;
cursor: pointer;
z-index: 1;
position: relative;
right: -27%;
}
#webCifar-icon svg {
max-width: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment