-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2419 from SrijaVuppala295/jsh
Created JS Hard challenge !!!
- Loading branch information
Showing
1 changed file
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>JavaScript Hard Quiz</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #08114c; | ||
margin: 0; | ||
padding: 20px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
height: 100vh; | ||
} | ||
|
||
.quiz-container { | ||
background-color: white; | ||
padding: 20px; | ||
border-radius: 10px; | ||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); | ||
max-width: 600px; | ||
width: 100%; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.question { | ||
margin-bottom: 15px; | ||
} | ||
|
||
.options { | ||
list-style-type: none; | ||
padding: 0; | ||
} | ||
|
||
.option { | ||
background-color: #e7f3fe; | ||
padding: 10px; | ||
border-radius: 5px; | ||
margin: 5px 0; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
border: 2px solid transparent; | ||
} | ||
|
||
.option:hover { | ||
background-color: #d1e7fd; | ||
} | ||
|
||
.selected { | ||
background-color: #b3e5fc; | ||
border: 2px solid #007bff; | ||
} | ||
|
||
button { | ||
background-color: #007bff; | ||
color: white; | ||
border: none; | ||
padding: 10px; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
display: block; | ||
width: 100%; | ||
margin-top: 10px; | ||
} | ||
|
||
button:hover { | ||
background-color: #0056b3; | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="quiz-container"> | ||
<h1>JavaScript Hard Quiz</h1> | ||
<div id="question-container"></div> | ||
<button id="next-button" class="hidden" onclick="nextQuestion()">Next Question</button> | ||
<button id="submit-button" class="hidden" onclick="submitAnswers()">Submit Answers</button> | ||
</div> | ||
|
||
<script> | ||
const questions = [ | ||
{ | ||
question: "1) What is the output of the following code: (1 + '1') * 2?", | ||
options: [ | ||
"2", | ||
"'11'", | ||
"12", | ||
"Error" | ||
], | ||
answer: 2 | ||
}, | ||
{ | ||
question: "2) What is the purpose of 'bind()' method in JavaScript?", | ||
options: [ | ||
"To bind event listeners", | ||
"To bind a function to a specific context", | ||
"To bind an object to a function", | ||
"None of the above" | ||
], | ||
answer: 1 | ||
}, | ||
{ | ||
question: "3) Which of the following is NOT a primitive data type in JavaScript?", | ||
options: [ | ||
"String", | ||
"Number", | ||
"Object", | ||
"Boolean" | ||
], | ||
answer: 2 | ||
}, | ||
{ | ||
question: "4) What is the output of the following code: typeof NaN?", | ||
options: [ | ||
"number", | ||
"undefined", | ||
"object", | ||
"NaN" | ||
], | ||
answer: 0 | ||
}, | ||
{ | ||
question: "5) How do you create a promise in JavaScript?", | ||
options: [ | ||
"Promise = new Promise()", | ||
"new Promise()", | ||
"create new Promise()", | ||
"None of the above" | ||
], | ||
answer: 1 | ||
} | ||
]; | ||
|
||
let currentQuestionIndex = 0; | ||
let score = 0; | ||
|
||
function showQuestion() { | ||
const questionContainer = document.getElementById('question-container'); | ||
const question = questions[currentQuestionIndex]; | ||
questionContainer.innerHTML = ` | ||
<div class="question"> | ||
<h3>${question.question}</h3> | ||
<ul class="options"> | ||
${question.options.map((option, i) => ` | ||
<li class="option" onclick="selectOption(this, ${i})">${option}</li> | ||
`).join('')} | ||
</ul> | ||
</div> | ||
`; | ||
document.getElementById('next-button').classList.add('hidden'); | ||
document.getElementById('submit-button').classList.add('hidden'); | ||
} | ||
|
||
function selectOption(optionElement, selectedIndex) { | ||
const options = document.querySelectorAll('.option'); | ||
options.forEach(option => option.classList.remove('selected')); | ||
optionElement.classList.add('selected'); | ||
|
||
const question = questions[currentQuestionIndex]; | ||
if (selectedIndex === question.answer) { | ||
score++; | ||
} | ||
|
||
document.getElementById('next-button').classList.remove('hidden'); | ||
} | ||
|
||
function nextQuestion() { | ||
currentQuestionIndex++; | ||
if (currentQuestionIndex < questions.length) { | ||
showQuestion(); | ||
} else { | ||
document.getElementById('next-button').classList.add('hidden'); | ||
document.getElementById('submit-button').classList.remove('hidden'); | ||
document.getElementById('question-container').innerHTML = `<h3>Challenge Complete! Your score is ${score} out of ${questions.length}</h3>`; | ||
} | ||
} | ||
|
||
function submitAnswers() { | ||
alert(`You scored ${score} out of ${questions.length}`); | ||
window.location.href = 'index.html'; // Redirect to main page after submitting | ||
} | ||
|
||
window.onload = showQuestion; | ||
</script> | ||
</body> | ||
</html> |