For my phase 4 project, I wanted to create a quiz. Who doesn’t like quizzes? Especially fun ones! Buzzfeed has the coolest ones if you were wondering.
One of the project requirements was to create a Rails API and have a belongs_to and has_many relationship. Using the Resource Generator is one of my favorite tools to use. After creating the database, tables, models, etc, the backend took no time to set up and establish relationships. In my project, the questions have many answers and the answers belongs to the questions.
In order to retrieve data, the Fetch API is used. The fetch() function takes in an argument of a URL. It will use a GET method for AJAX requests. It will default to a get request if we do not specify in the options. After the request is made, .then() method is used to convert the returned response into a JSON format. The second .then() uses the data for DOM manipulation.
The fetch API allowed me to retrieve my data from the backend and create my questions and answers for the quiz
Another tool I enjoyed using was onclick(). The onclick event happens when the user clicks an element on the web page and something happens. Inside the function, with one click, multiple actions are set to occur on the page. In my project, I used onclick() on my answer buttons. When the user chooses an answer, it will turn green for correct or red for incorrect. The other answer buttons become disabled. It will also add to their score if the answer is correct.