danglove113 / quiz-app

Creating a quiz app using HTML, CSS, and Javascript from a tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

quiz-app

Creating a quiz app using HTML, CSS, and Javascript from this tutorial https://www.youtube.com/watch?v=zZdQGs62cR8&index=5&list=PLDlWc9AfQBfZIkdVaOQXi1tizJeNJipEx

Objective

I've been working on a lot of backend stuff and wanted to do a project focused on front-end web design and development. I found this tutorial from James Q Quick.

Build a Quiz App (2) - Create and Style the Game Page

In this section we create game.html and game.css.

Build a Quiz App (3) - Display Hard Coded Questions

video link

  • Create game.js file
  • Create functions that load questions, identify selected answers

Need to figure out how to query each of the .choice-text elements, so using data-number="X" as noted in the pic below. code snippet

Then by adding const choices = document.getElementsByClassName("choice-text"); we get an HTMLCollection like the following output to the console:

We then change that to an array using const choices = Array.from(document.getElementsByClassName("choice-text")); and it changes the output to this:

New Things

This section uses the JavaScript Spread syntax to bring the questions array into the availableQuestions array like this

This also uses the splice method (which I've used before, but it's been a while) which adds/removes items to/from an array.

Build a Quiz App (4) - Display Feedback for Correct/Incorrect Answers

video link

What we did in this video:

  • Apply a class to the question element based on correct/incorrect answer selection
  • use setTimeout to display the correct/incorrect answer for 1000ms before calling getNewQuestion();

Build a Quiz App (5) - Create a Head's Up Display

video link

What we did in this video:

  • Add the HTML/CSS elements to create a HUD (Question and Score)
  • Create function to increment score if answer is correct.

Build a Quiz App (6) - Create a Progress Bar

video link

What we did in this video:

  • Removed old HTML element showing question numbers
  • Added 2
    s, one for the outline of the progress bar, one for the progress bar itself
  • The progress bar is filled in by adjusting the width of inner div using rogressBarFull.style.width = ${(questionCounter / MAX_QUESTIONS) * 100}%;

Build a Quiz App (7) - Create and Style the End Page

video link

What we did in this video:

  • Created end.html and end.js files
  • Added CSS for forms
  • Added function to end.js that checks for a value in the username field to disable to the save button
  • Saves player's score to local storage localStorage.setItem("mostRecentScore", score);

To look at local storage in Chrome, go to Application tab > Local Storage

Build a Quiz App (8) - Save High Scores in Local Storage

video link

What we did in this video:

  • Figure out how to sort high scores, limit scores to top 5, and write them to local storage

Build a Quiz App (9) - Load and Display High Scores from Local Storage

video link

  • Creates highscores.html, highscores.css, and highscores.js pages
  • Pulls in high scores from local storage using const highScores = JSON.parse(localStorage.getItem('highscores')) || [];
  • Use JS array map() to convert the returned array from local storage to elements for an HTML list

Build a Quiz App (10) - Fetch API to Load Local Questions

video link

  • Move hard coded questions from game.js to questions.json

Use this code pull pull questions from questions.json and display them in the console:

When I do that, I get an HTTP response 200:

To make this a usble JSON response I use the following code:

And here's what the final code looks like to pull questions from questions.json, which also has a .catch to handle any errors. It also moves the startGame() function to be called after the questions are loaded.

Build a Quiz App (11) - Fetch API to Load Questions API

video link

Uses Open Trivia DB API to pull questions.

Configuring questions configuring questions

Side note: I installed JSON Formatter into Chrome to be able to view the returned JSON in a cleaner format.

The format in which questions are returned from Open Trivia DB is not the format we use in our app. So we're going to use a map function to clean it up.

This is what that section of code looks like now:

Build a Quiz App (12) - Create a Spinning Loader

video link

Now that we're pulling questions from an API, there is a moment when game.html loads that it shows the default text. This section will resolve that issue by adding a loader.

Creates a new HTML element called <div id="loader"></div> in which we use a CSS animation to create a spinning circle. This was taken from w3schools | CSS Loader

'game.js' is modififed so where the loader is removed and the game content is revealed upon the start of the game (once the questions have loaded);

About

Creating a quiz app using HTML, CSS, and Javascript from a tutorial


Languages

Language:JavaScript 41.3%Language:HTML 32.1%Language:CSS 26.6%