let questions = [
{
prompt: `Inside which HTML
element
do
we put
the JavaScript?`,
options: [
"<javascript>"
,
"<js>"
,
"<script>"
,
"<scripting>"
,
],
answer:
"<script>"
,
},
{
prompt: `How
do
you call a
function
named
myFunction?`,
options: [
"call myFunction()"
,
"myFunction()"
,
"call function myFunction"
,
"Call.myFunction"
,
],
answer:
"myFunction()"
,
},
{
prompt: `How does a
for
loop
start?`,
options: [
"for (i = 0; i <= 5; i++)"
,
"for (i = 0; i <= 5)"
,
"for i = 1 to 5"
,
" for (i <= 5; i++)"
,
],
answer:
"for (i = 0; i <= 5; i++)"
,
},
{
prompt: `In JavaScript, which
of the following is
a logical operator?`,
options: [
"|"
,
"&&"
,
"%"
,
"/"
],
answer:
"&&"
,
},
{
prompt: `A named element
in
a
JavaScript program that
is used to store and
retrieve data is a _____.`,
options: [
"method"
,
"assignment operator"
,
"letiable"
,
"string"
,
],
answer:
"letiable"
,
},
];
let questionsEl =
document.querySelector(
"#questions"
);
let timerEl =
document.querySelector(
"#timer"
);
let choicesEl =
document.querySelector(
"#options"
);
let submitBtn = document.querySelector(
"#submit-score"
);
let startBtn =
document.querySelector(
"#start"
);
let nameEl =
document.querySelector(
"#name"
);
let feedbackEl = document.querySelector(
"#feedback"
);
let reStartBtn =
document.querySelector(
"#restart"
);
let currentQuestionIndex = 0;
let time = questions.length * 15;
let timerId;
function
quizStart() {
timerId = setInterval(
clockTick,
1000
);
timerEl.textContent = time;
let landingScreenEl =
document.getElementById(
"start-screen"
);
landingScreenEl.setAttribute(
"class"
,
"hide"
);
questionsEl.removeAttribute(
"class"
);
getQuestion();
}
function
getQuestion() {
let currentQuestion =
questions[currentQuestionIndex];
let promptEl =
document.getElementById(
"question-words"
);
promptEl.textContent =
currentQuestion.prompt;
choicesEl.innerHTML =
""
;
currentQuestion.options.forEach(
function
(choice, i) {
let choiceBtn =
document.createElement(
"button"
);
choiceBtn.setAttribute(
"value"
,
choice
);
choiceBtn.textContent =
i + 1 +
". "
+ choice;
choiceBtn.onclick =
questionClick;
choicesEl.appendChild(
choiceBtn
);
}
);
}
function
questionClick() {
if
(
this
.value !==
questions[currentQuestionIndex]
.answer
) {
time -= 10;
if
(time < 0) {
time = 0;
}
timerEl.textContent = time;
feedbackEl.textContent = `Wrong! The correct answer was
${questions[currentQuestionIndex].answer}.`;
feedbackEl.style.color =
"red"
;
}
else
{
feedbackEl.textContent =
"Correct!"
;
feedbackEl.style.color =
"green"
;
}
feedbackEl.setAttribute(
"class"
,
"feedback"
);
setTimeout(
function
() {
feedbackEl.setAttribute(
"class"
,
"feedback hide"
);
}, 2000);
currentQuestionIndex++;
if
(
currentQuestionIndex ===
questions.length
) {
quizEnd();
}
else
{
getQuestion();
}
}
function
quizEnd() {
clearInterval(timerId);
let endScreenEl =
document.getElementById(
"quiz-end"
);
endScreenEl.removeAttribute(
"class"
);
let finalScoreEl =
document.getElementById(
"score-final"
);
finalScoreEl.textContent = time;
questionsEl.setAttribute(
"class"
,
"hide"
);
}
function
clockTick() {
time--;
timerEl.textContent = time;
if
(time <= 0) {
quizEnd();
}
}
function
saveHighscore() {
let name = nameEl.value.trim();
if
(name !==
""
) {
let highscores =
JSON.parse(
window.localStorage.getItem(
"highscores"
)
) || [];
let newScore = {
score: time,
name: name,
};
highscores.push(newScore);
window.localStorage.setItem(
"highscores"
,
JSON.stringify(highscores)
);
alert(
"Your Score has been Submitted"
);
}
}
function
checkForEnter(event) {
if
(event.key ===
"Enter"
) {
saveHighscore();
alert(
"Your Score has been Submitted"
);
}
}
nameEl.onkeyup = checkForEnter;
submitBtn.onclick = saveHighscore;
startBtn.onclick = quizStart;