diff --git a/QuizApp/yashwanthvarma18/brickBreaker.code-workspace b/QuizApp/yashwanthvarma18/brickBreaker.code-workspace new file mode 100644 index 000000000..e9793da78 --- /dev/null +++ b/QuizApp/yashwanthvarma18/brickBreaker.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "../brickBreaker" + }, + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/QuizApp/yashwanthvarma18/index.html b/QuizApp/yashwanthvarma18/index.html new file mode 100644 index 000000000..310af9585 --- /dev/null +++ b/QuizApp/yashwanthvarma18/index.html @@ -0,0 +1,107 @@ + + + +
+ + +Loading...
`); + createQuestions(numQ, diff, cat); + }); + + // Event handler for the "Next" button + $('.box').on('click', '#next', function() { + event.preventDefault(); + handleNext(); + }); + + // Event handler for the "Submit" button (when guessing) + $('.box').on('click', '#guess', function() { + event.preventDefault(); + handleGuess(); + }); + + // Event handler for the "New Quiz" button (restart) + $('.box').on('click', '#restart', function() { + event.preventDefault(); + STORE = []; // Clear questions before starting a new game + restartQuiz(); + }); +} + +// Uses fetch to utilize the API and sends the JSON data to the testData function +function createQuestions(numQ, diff, cat) { + let url = `https://opentdb.com/api.php?amount=${numQ}&type=multiple${diff}${cat}`; + fetch(url) + .then((response) => response.json()) + .then((responseJson) => testData(responseJson, numQ, diff, cat)) + .catch((error) => alert(error)); +} + +// This makes sure the API has enough questions for requested specifications +function testData(data, numQ, diff, cat) { + if (data.response_code === 0) { + createArr(data); // If enough questions are available, create an array of questions + } else { + numQ--; + createQuestions(numQ, diff, cat); // Try again with fewer questions + } +} + +// creates the array of questions using the JSON data +function createArr(data) { + for (let question in data.results) { + let arr = []; + + // Create an array of potential answers + arr.push(data.results[question].correct_answer); + arr.push(data.results[question].incorrect_answers[0]); + arr.push(data.results[question].incorrect_answers[1]); + arr.push(data.results[question].incorrect_answers[2]); + + // Shuffle the order of the answers + for (let i = arr.length - 1; i > 0; i--) { + let j = Math.floor(Math.random() * (i + 1)); + [arr[i], arr[j]] = [arr[j], arr[i]]; + } + + // Create an object containing the question, array of potential answers, and actual answer + let newObj = { + question: data.results[question].question, + answers: arr, + rightAnswer: data.results[question].correct_answer + }; + STORE.push(newObj); + } + renderQuestion(); // Display the first question +} + +// Renders the question form +function renderQuestion() { + $('.box').html( + ` + +Test
+"Please select an answer."
+ ` + ); +} + +// Handles when someone guesses an answer +function handleGuess() { + // Checks to see if an option is checked + if ($("input[name='answer']:checked").data('value')) { + guess(); + } else { + // Shows the error message if nothing is checked + if ($('#error').hasClass('hidden')) { + $('#error').toggleClass('hidden'); + } + } +} + +// Checks whether answer is correct or not +function guess() { + if (!$('#error').hasClass('hidden')) { + $('#error').toggleClass('hidden'); + } + + // Disables ability to guess again on the current question + $('input[type=radio]').attr('disabled', true); + + // Handle special characters in answers + let encodedStr = STORE[currentQuestion].rightAnswer; + let parser = new DOMParser(); + let dom = parser.parseFromString('' + encodedStr, 'text/html'); + let decodedString = dom.body.textContent; + + correctMessage = `That is correct! Way to go!`; + incorrectMessage = `That is incorrect. The correct answer is: ${decodedString}.`; + + if ($("input[name='answer']:checked").data('value') == decodedString) { + $('#message').text(`${correctMessage}`); + correctAnswers++; + } else { + $('#message').text(`${incorrectMessage}`); + incorrectAnswers++; + } + toggleClasses(); // Toggle messages for correct/incorrect and update the score +} + +// Toggles the messages for correct/incorrect and shows the new score +function toggleClasses() { + $('#message').toggleClass('hidden'); + $('#guess').toggleClass('hidden'); + $('#next').toggleClass('hidden'); + $('.scoreboard').html(scores()); // Update the scoreboard +} + +// Handles when someone presses the next button +function handleNext() { + if (questionNumber != STORE.length) { + currentQuestion++; + questionNumber++; + renderQuestion(); // Display the next question + } else { + $('.box').html(results()); // Display the results when all questions are answered + } +} + +// This is the display for the results page. +function results() { + // Messages given based on the score + let goodScore = ' You really know your trivia!'; + let okayScore = ' Try again and see if you can do better!'; + let badScore = ' Better Luck Next Time.'; + + let scoreMessage; + + if (correctAnswers > STORE.length * 0.7) { + scoreMessage = ' 😎👌' + goodScore; + } else if (correctAnswers > STORE.length * 0.4) { + scoreMessage = ' 😉' + okayScore; + } else { + scoreMessage = ' 😵' + badScore; + } + + const resultBox = ` ++ You got ${correctAnswers} out of ${STORE.length} questions correct. ${scoreMessage} +
+ +