[en] app.js - GhostyJade/ProgettoJS-API-2019 GitHub Wiki

app.js

This script manage all the game logic. It's the responsable to fetch images from Unsplash and questions from Open Trivia DB.

Global variables

  • data: array that stores all questions' data

  • theCorrectAnswer: variable that stores the correct answer

  • unsplashApiKey: Unsplash API key (i know that it isn't a good practice to store API key as public, but I hadn't found any API that is at the same level as Unsplash)

  • image: the image shown when playing

  • text: the question's text

  • btnNextQuestion: the button used to skip a question or to go to the next question, after submitting

  • btnCheckQuestion: the button used to submit the answer

  • options: the form object (used to show the answers)

  • result: a reference to the object used to show the result (correct/wrong)

  • masterContainer: the master container, it contain all the elements (excluding header and footer)

  • subContainer: the sub container (child of masterContainer)

  • onscreenScore: an object that show the on screen score

  • score: variable that store the player score

  • currentQuestion: variable that store how many question has been submitted

  • totalQuestions: value that stores how many questions should be fetched

  • submitted: a boolean value that tell if a given question has been submitted or not

Metodi / funzioni

  • shuffle: function that shuffle the givven array
  • getQuestions: function that fetch data from OpenTriviaDB
  • createRadioElement: function that create a radio button and set it's content (data)
  • recreateButtons: add Confirm and Next
  • dispatchQuestion: a function that reset the game UI and dispatch a question
  • getImage: function that fetch an image from Unsplash and set it to it's container
  • createAlert: create the element shown when the user submit a question (Correct / Wrong)
  • checkAnswer: check if the answer is correct
  • initButtons: create the button instances
  • updateOnScreenScore: update the onscreen scores
  • elementFactory: create an html tag from given type, content (data) and class list (classAttribs)
  • showScores:show scores at the end of the game