Skip to content

Added Quiz Web-App #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Quiz-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz Web App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="panel">
<h1>Quiz</h1>
<hr>
<div class="question" id="ques"></div>
<div class="options" id="opt"></div>
<button onclick="checkAns()" id="btn">SUBMIT</button>
<div id="score"></div>
<script src="script.js"></script>
</div>
</body>
</html>
88 changes: 88 additions & 0 deletions Quiz-app/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const Questions = [{
q: "What is capital of India?",
a: [{ text: "Gandhinagar", isCorrect: false },
{ text: "Surat", isCorrect: false },
{ text: "Delhi", isCorrect: true },
{ text: "Mumbai", isCorrect: false }
]

},
{
q: "What is the capital of Thailand?",
a: [{ text: "Lampang", isCorrect: false, isSelected: false },
{ text: "Phuket", isCorrect: false },
{ text: "Ayutthaya", isCorrect: false },
{ text: "Bangkok", isCorrect: true }
]

},
{
q: "What is the capital of Gujarat",
a: [{ text: "Surat", isCorrect: false },
{ text: "Vadodara", isCorrect: false },
{ text: "Gandhinagar", isCorrect: true },
{ text: "Rajkot", isCorrect: false }
]

}

]

let currQuestion = 0
let score = 0

function loadQues() {
const question = document.getElementById("ques")
const opt = document.getElementById("opt")

question.textContent = Questions[currQuestion].q;
opt.innerHTML = ""

for (let i = 0; i < Questions[currQuestion].a.length; i++) {
const choicesdiv = document.createElement("div");
const choice = document.createElement("input");
const choiceLabel = document.createElement("label");

choice.type = "radio";
choice.name = "answer";
choice.value = i;

choiceLabel.textContent = Questions[currQuestion].a[i].text;

choicesdiv.appendChild(choice);
choicesdiv.appendChild(choiceLabel);
opt.appendChild(choicesdiv);
}
}

loadQues();

function loadScore() {
const totalScore = document.getElementById("score")
totalScore.textContent = `You scored ${score} out of ${Questions.length}`
}


function nextQuestion() {
if (currQuestion < Questions.length - 1) {
currQuestion++;
loadQues();
} else {
document.getElementById("opt").remove()
document.getElementById("ques").remove()
document.getElementById("btn").remove()
loadScore();
}
}

function checkAns() {
const selectedAns = parseInt(document.querySelector('input[name="answer"]:checked').value);

if (Questions[currQuestion].a[selectedAns].isCorrect) {
score++;
console.log("Correct")
nextQuestion();
} else {
nextQuestion();
}
}
61 changes: 61 additions & 0 deletions Quiz-app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}

html, body{
height: 100vh;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(60deg,#00a2ff,#c813ff);
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.panel{
background: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
width: 50rem;
height: auto;
padding: 50px 50px 50px 50px;
}

.panel>h1{
margin-bottom: 20px;
display: block;
text-align: center;
}

.panel>hr{
margin-bottom: 20px;
}

.panel .question{
font-weight: 500;
font-size: 20px;
margin-bottom: 10px;
}

.panel .options{
display: grid;
grid-gap: 15px;
margin-bottom: 30px;
}

#btn{
background: linear-gradient(60deg,#00a2ff,#c813ff);
border: none;
padding: 15px 50px;
margin: 0 auto;
display: block;
color: #fff;
font-weight: 600;
transition: background 0.5s ease-in;
}

#btn:hover{
background: linear-gradient(120deg,#00a2ff,#c813ff);
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
}