const start_btn = document.querySelector(".start_btn button"); const info_box = document.querySelector(".info_box"); const exit_btn = info_box.querySelector(".buttons .quit"); const continue_btn = info_box.querySelector(".buttons .restart"); const quiz_box = document.querySelector(".quiz_box"); const result_box = document.querySelector(".result_box"); const option_list = document.querySelector(".option_list"); const time_line = document.querySelector("header .time_line"); const timeText = document.querySelector(".timer .time_left_txt"); const timeCount = document.querySelector(".timer .timer_sec"); const infm=quiz_box.querySelector(".info"); start_btn.onclick = ()=>{ info_box.classList.add("activeInfo"); } exit_btn.onclick = ()=>{ info_box.classList.remove("activeInfo"); } continue_btn.onclick = ()=>{ info_box.classList.remove("activeInfo"); quiz_box.classList.add("activeQuiz"); showQuetions(0); queCounter(1); startTimer(15); startTimerLine(0); } let timeValue = 15; let que_count = 0; let que_numb = 1; let userScore = 0; let counter; let counterLine; let widthValue = 0; const restart_quiz = result_box.querySelector(".buttons .restart"); const quit_quiz = result_box.querySelector(".buttons .quit"); restart_quiz.onclick = ()=>{ quiz_box.classList.add("activeQuiz"); result_box.classList.remove("activeResult"); timeValue = 15; que_count = 0; que_numb = 1; userScore = 0; widthValue = 0; showQuetions(que_count); queCounter(que_numb); clearInterval(counter); clearInterval(counterLine); startTimer(timeValue); startTimerLine(widthValue); timeText.textContent = "Time Left"; next_btn.classList.remove("show"); } quit_quiz.onclick = ()=>{ window.location.reload(); } const quit_btn = document.querySelector("footer .quit_btn"); quit_btn.onclick = ()=>{ quiz_box.classList.remove("activeQuiz"); } const next_btn = document.querySelector("footer .next_btn"); const bottom_ques_counter = document.querySelector("footer .total_que"); next_btn.onclick = ()=>{ if(que_count < questions.length - 1){ que_count++; que_numb++; showQuetions(que_count); queCounter(que_numb); clearInterval(counter); clearInterval(counterLine); startTimer(timeValue); startTimerLine(widthValue); timeText.textContent = "Time Left"; next_btn.classList.remove("show"); }else{ clearInterval(counter); clearInterval(counterLine); showResult(); } infm.innerHTML=""; } function showQuetions(index){ const que_text = document.querySelector(".que_text"); let que_tag = ''+ questions[index].numb + ". " + questions[index].question +''; let option_tag = '
'+ userScore +'
out of'+ questions.length +'
'; scoreText.innerHTML = scoreTag; const attemptSummary = document.createElement('div'); attemptSummary.innerHTML = `Note: First Load questions then Click The Print Button
`; scoreText.appendChild(attemptSummary); } else if(userScore > 1){ let scoreTag = 'and nice , You got'+ userScore +'
out of'+ questions.length +'
'; scoreText.innerHTML = scoreTag; const attemptSummary = document.createElement('div'); attemptSummary.innerHTML = `Note: First Load questions then Click The Print Button
`; scoreText.appendChild(attemptSummary); } else{ let scoreTag = 'and sorry , You got only'+ userScore +'
out of'+ questions.length +'
'; scoreText.innerHTML = scoreTag; const attemptSummary = document.createElement('div'); attemptSummary.innerHTML = `Note: First Load questions then Click The Print Button
`; scoreText.appendChild(attemptSummary); } } function startTimer(time){ counter = setInterval(timer, 1000); function timer(){ timeCount.textContent = time; time--; if(time < 9){ let addZero = timeCount.textContent; timeCount.textContent = "0" + addZero; } if(time < 0){ clearInterval(counter); timeText.textContent = "Time Off"; const allOptions = option_list.children.length; let correcAns = questions[que_count].answer; for(i=0; i < allOptions; i++){ if(option_list.children[i].textContent == correcAns){ option_list.children[i].setAttribute("class", "option correct"); option_list.children[i].insertAdjacentHTML("beforeend", tickIconTag); console.log("Time Off: Auto selected correct answer."); } } for(i=0; i < allOptions; i++){ option_list.children[i].classList.add("disabled"); } next_btn.classList.add("show"); } } } function startTimerLine(time){ counterLine = setInterval(timer, 100); function timer(){ time += 4.9; time_line.style.width = time + "px"; if(time > 745){ clearInterval(counterLine); } } } function queCounter(index){ let totalQueCounTag = ''+ index +'
of'+ questions.length +'
Questions'; bottom_ques_counter.innerHTML = totalQueCounTag; } //////////// document.getElementById("print-btn").onclick = () => { // Create a div element to hold the printable content const printableContent = document.createElement("div"); printableContent.id = "printable-content"; printableContent.style.padding = "20px"; printableContent.style.fontFamily = "Arial, sans-serif"; // Loop through each question and create HTML structure questions.forEach(question => { const questionDiv = document.createElement("div"); questionDiv.classList.add("question"); questionDiv.style.marginBottom = "20px"; const questionText = document.createElement("p"); questionText.textContent = `${question.numb}. ${question.question}`; questionDiv.appendChild(questionText); question.options.forEach(option => { const optionText = document.createElement("p"); optionText.textContent = option; questionDiv.appendChild(optionText); }); const answerText = document.createElement("p"); answerText.textContent = `Correct Answer: ${question.answer}`; questionDiv.appendChild(answerText); const infoText = document.createElement("p"); infoText.textContent = `Info: ${question.inf}`; questionDiv.appendChild(infoText); printableContent.appendChild(questionDiv); }); // Append the printable content to the body (invisible) document.body.appendChild(printableContent); // Use html2canvas to create a canvas of the printable content html2canvas(printableContent).then(canvas => { const imgData = canvas.toDataURL("image/png"); const pdf = new jsPDF("p", "pt", "a4"); const imgProps = pdf.getImageProperties(imgData); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight); pdf.save("quiz.pdf"); // Remove the printable content from the body document.body.removeChild(printableContent); }); // Initialize variables to count attempted questions and correct answers let attemptedQuestions = 0; //let correctAnswers = 0; // Create a variable to store the HTML content for displaying questions, chosen answers, correct answers, and correctness let userAnswersHTML = ''; // Loop through each question questions.forEach((question, index) => { // Increment attempted questions count attemptedQuestions++; // Add question text userAnswersHTML += `Attempted ${attemptedQuestions} out of ${questions.length} questions.
`; scoreText.appendChild(attemptSummary); };