{"id":3784,"date":"2023-07-18T01:28:59","date_gmt":"2023-07-18T01:28:59","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3784"},"modified":"2026-03-16T19:41:53","modified_gmt":"2026-03-16T19:41:53","slug":"exercise-trivia","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-seven\/exercise-trivia\/","title":{"rendered":"Exercise: Trivia!"},"content":{"rendered":"\n<p><a href=\"https:\/\/the-trivia-api.com\/\">The Trivia API<\/a>, the api that forms the basis for this lesson offers access to lots of Trivia Questions! Not surprisingly, these questions cover a broad range of topics and can be accessed randomly or by categories. <\/p>\n\n\n\n<p>Check out the questions along with parameters like category, id, correct answer, incorrect answers, difficulty, etc. at <a href=\"https:\/\/the-trivia-api.com\/v2\/questions\/\">https:\/\/the-trivia-api.com\/v2\/questions\/<\/a>. <\/p>\n\n\n\n<p>The object has a lot of information but most importantly for the trivia Q and A we are about to construct, are the question, answer, and category title properties.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"556\" src=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.25.18\u202fAM-1024x556.png\" alt=\"\" class=\"wp-image-4140\" srcset=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.25.18\u202fAM-1024x556.png 1024w, https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.25.18\u202fAM-300x163.png 300w, https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.25.18\u202fAM-768x417.png 768w, https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.25.18\u202fAM.png 1286w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Let&#8217;s start by constructing our basic html document. Name your document randomTrivia.html.&nbsp; We want a document that will display the category title and the question, and afterward, supply the correct answer. The screenshot below shows the path that I took and you are welcome to copy it verbatim. As you can see, I will be referring to a function named qa that will load a new question, followed by another function name showAnswer.<img decoding=\"async\" src=\"http:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.31.43\u202fAM.png\" alt=\"\"><\/li>\n\n\n\n<li>Next step is to create two empty functions, qa and showAnswer, as well as variables that connect to the question, category, and answer div tags as shown below. You will note as well that we create on additional empty string variable named theAnswer. We will talk about that one later. For now, just add it. <pre class=\"wp-block-code\"><code>let question = document.querySelector(\"#question\");\nlet answer = document.querySelector(\"#answer\");\nlet category = document.querySelector(\"#category\");\nlet theAnswer = \"\";\n\nfunction qa() {\n     \n\n}\n\nfunction showAnswer() {\n\n\n}<\/code><\/pre><\/li>\n\n\n\n<li>We will be using Jquery&#8217;s $.getJSON function, so visit&nbsp;<a href=\"https:\/\/developers.google.com\/speed\/libraries\/\">Googles&#8217; hosted libraries<\/a>&nbsp;and paste in the code that they supply to load the latest version (3.7.1) of jquery. (within your head tags) <\/li>\n\n\n\n<li>The next step is the use the $.getJSON to load our data. Copy and paste the code below within your qa function. <pre class=\"wp-block-code\"><code> $.getJSON('https:\/\/the-trivia-api.com\/v2\/questions\/', function(data) { });<\/code><\/pre><\/li>\n\n\n\n<li>Now modify your qa function as shown below, to add the value of the data object&#8217;s question to your #question div tag. Please take note that we first have to reference the one item array with data[0] which leads us to the object that is encased in {} brackets. The question is only one level deep within the object so all we have to do to get its value is to link it to data[0] with dot syntax. <pre class=\"wp-block-code\"><code>function qa() {\n     $.getJSON('https:\/\/the-trivia-api.com\/v2\/questions\/', function(data) { \n\n        question.innerHTML = data[0].question.text;\n\n     });\n\n}<\/code><\/pre><\/li>\n\n\n\n<li>Now let&#8217;s add the category title to our #category div tag. Each randomly generated object (so far) comes with a category title which may serve as a useful hint to your users when they are trying to figure out the answer. (<em>Follow the same strategy that you used to get the value of the question into the #question div<\/em>) <img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"197\" src=\"http:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/03\/Screenshot-2025-03-17-at-10.48.41\u202fAM.png\" alt=\"\"><\/li>\n\n\n\n<li>If you were able to follow my bread crumb trail, you should be seeing text in the category field as well as in the question field. But how does your user know that it&#8217;s a category? Maybe you should add some explanatory text of your own. Hint:&nbsp;<strong>innerHTML = &#8220;Category: &#8220;+ data[0].etc<\/strong>&nbsp;(don&#8217;t take the etc literally :).<\/li>\n\n\n\n<li>Now that our categories and questions appear, we need answers. Running the $.getJSON function again in yet another function would be a terrible idea because it would provide a random answer to a random question!&nbsp; What we need to do is capture the value of answer and then use it later in another function to provide feedback to our user. For that job we will use the variable that we created earlier named&nbsp;<strong>theAnswer .&nbsp;<\/strong>We created that variable outside of our qa function so that it would be what is referred to as a global variable. Variables that are created within a function are referred to as local variables, and have meaning only within the function. Global variables are always available. So do it by adding this line to your qa function:&nbsp;&nbsp;<strong>theAnswer = data[0].correctAnswer;<\/strong><\/li>\n\n\n\n<li>Now create another function named&nbsp;<strong>showAnswer<\/strong>&nbsp;that sets the&nbsp;<strong>innerHTML<\/strong>&nbsp;of the&nbsp;<strong>#answer div<\/strong>&nbsp;to the value of the&nbsp;<strong>theAnswer<\/strong>&nbsp;variable that you created in the previous step.<\/li>\n<\/ol>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Hint<\/summary>\n<pre class=\"wp-block-code\"><code>function showAnswer() {\n\n    answer.innerHTML = \"Answer: \"+ theAnswer;\n\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p>How do I get the &#8220;_&#8221; underscores to disappear from my categories?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>category.innerHTML = \"Category: \" + data&#091;0].category.replaceAll(\"_\", \" \");<\/code><\/pre>\n\n\n\n<p>Does everything work? If so, good, but I bet it looks awful. Your final task is to use&nbsp;a bit of CSS to tidy everything up and make it look better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Trivia API, the api that forms the basis for this lesson offers access to lots of Trivia Questions! Not surprisingly, these questions cover a broad range of topics and can be accessed randomly or by categories. Check out the questions along with parameters like category, id, correct answer, incorrect answers, difficulty, etc. at https:\/\/the-trivia-api.com\/v2\/questions\/. &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-seven\/exercise-trivia\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Trivia!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3696,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3784","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3784"}],"version-history":[{"count":26,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3784\/revisions"}],"predecessor-version":[{"id":4804,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3784\/revisions\/4804"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3696"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}