Project: Space Quiz
No new theory: this project uses everything from part 8 and before it. fetch and JSON, async and await, try and catch, destructuring, buttons made by a script, and a live region. You build your own page from an empty folder: a quiz that loads its questions from a file.
This project is separate from the station site. When you're not sure how to do a step, open the lesson it comes from again.
Your files
The folder starts empty. Make four files with New file: index.html, style.css, quiz.js and questions.json.
What you will see
The page loads its questions, then shows them one at a time: Question 1 of 3, the question, and a button for each answer. A click on an answer says if it's right, and a Next button takes you on. At the end, the page says your score.
Why the questions are in a file
You could write the questions in the script. But then changing a question means changing the code. With the questions in their own file, anyone can add one without touching the script. The page loads them with fetch, the way it would load them from a server. A path of your own site works with fetch like any address: fetch("questions.json").
The steps
Build it in this order:
-
The frame. An English page that can show any letter and is made for the width of a phone, with one sentence about the quiz for search engines. It's called Space Quiz on its tab and in its only biggest heading.
-
The questions.
questions.jsonholds an array of at least three questions about space. Each one is an object with thequestion, itsanswers(an array of texts) andcorrect: the place in that array of the right answer, counted from 0. Like this:[{ "question": "Which star is the closest to Earth?", "answers": ["Sirius", "The Sun"], "correct": 1 }] -
The parts. Under the heading: a paragraph with the id
quiz-status, a heading with the idquestion, a box with the idanswers, a paragraph with the idfeedback, and a button of typebuttonwith the idnext, hidden. A screen reader reads#quiz-statusand#feedbackout politely when they change. -
Loading. The head loads
quiz.js, which waits for the page. It loadsquestions.jsonwithfetchwhen the page opens. When the file is missing, or isn't JSON,#quiz-statussaysCouldn't load the questions:, then the error's message, and nothing is thrown. -
A question. When the questions are loaded, the first question shows.
#quiz-statussaysQuestion 1 of 3(with your own number of questions),#questionholds the question, and#answersholds a button of typebuttonfor each answer, in the file's order. -
Answering. A click on an answer says in
#feedbackRight!, orNot quite: the answer isand the right answer. Then nobody can press an answer of that question any more, and#nextshows. -
The next question. A click on
#nextshows the next question the same way, with#feedbackempty and#nexthidden again. -
The score. After the last question,
#nextshows the score instead.#quiz-statussaysYou got 2 of 3 right(with the real numbers), and the question, the answers and#nextare gone. -
On a phone. The answers stand one under the other, easy to read with a contrast of at least 4.5, and nothing comes out sideways.
The tests also try your page with their own questions in questions.json. So the page must work with any questions written the same way.
Run the tests when you like. They tell you which steps are still missing.