Giter VIP home page Giter VIP logo

quizard's Introduction

Quizard

A vanilla JavaScript, Single Page Web App that lets you test your knowledge in a range of subjects.

Table of Contents

General Info

Quizard is a single page JavaScript web app which allows users to play a quiz game, create and login to accounts, and save quiz scores to compare scores on different quizzes.

Inspiration

Quizard was created as a sample project to explore designing vanilla JavaScript single page apps while integrating outside APIs for data in addition to a backend with auth and user permissions.

Demonstration Video

Quizard Youtube Demonstation

Technologies

  • Ruby on Rails
  • ActiveRecord
  • JavaScript
  • Sqlite3
  • bcrypt
  • JWT
  • HTML
  • SCSS

Setup

To get Quizard installed and running, first clone the Github Repository into your directory and navigate to the backend folder to install the required gems and get the database set up:

cd backend
bundle install
rails db:migrate

Then start up the rails server - notice that the app is setup to run the backend on port 3001:

rails s -p 3001

To get the front end running, navigate into the frontend folder and run your server (I'm using lite-server)

~frontend lite-server

Example Code

  def login
    @user = User.find_by username: params[:username]

    if !@user
      render json: { error: 'Authentication failed.' }, status: :unauthorized
    else
      if !@user.authenticate params[:password]
        render json: { error: 'Authentication failed!' }, status: :unauthorized
      else
        payload = { user_id: @user.id }
        secret = Rails.application.secret_key_base
        token = JWT.encode payload, secret
        render json: { token: token, user: @user }
      end
    end
  end

  def authorize
    header = request.headers['Authorization']
    puts request.headers['Authorization']
    token = header.split(' ')[1]
    if !token
      render json: { error: 'User not authorized' }, status: :forbidden
    else
      begin
        secret = Rails.application.secret_key_base
        payload = JWT.decode(token, secret)[0]
        @user = User.find payload['user_id']
      rescue StandardError
        render json: { error: 'User not authorized' }, status: :forbidden unless @user
      end
    end
  end
function selectAnswer(event) {
  const correctAnswer = event.target.parentNode.parentNode.getAttribute('data-correct-answer');
  if (event.target.textContent === correctAnswer) {
    totalCorrect += 1;
    $.answerTotal.textContent = totalCorrect;
    event.target.style.backgroundColor = "#3fa649";
  } else {
    event.target.style.backgroundColor = "#e63743";
  }
  if (currentQuestionIndex < shuffledQuestions.length - 1) {
    $.nextButton.classList.remove('hidden');
  } else {
    setTimeout(function () { endGame(totalCorrect) }, 1000);
  }

funtion UserLogin(event) {
  event.preventDefault();
  const formData = new FormData(event.target);
  const username = formData.get('username');
  const password = formData.get('password');
  const user = {
    username,
    password
  };
  fetch(db.login, {
    method: 'POST',
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(user)
  }).then(renderJSON)
    .then(handleResponse)
    .catch(errorAlert);
}

function handleResponse(response) {
  if (response.error) {
    alert(`Error! ${response.error}`)
  } else {
    grantUserAccess(response.token)
    storeUserData(response.user)
  }
}

Features

Current Features:

  • Play as a guest user or create an account to keep coming back
  • Full auth
  • Select specific categories to quiz yourself on
  • Save scores to your user account

Future Features:

  • Timer to track how long your quiz took
  • Add more categories to be quizzed on
  • Create a leaderboard with public high scores
  • Ability to choose how many questions appear on the quiz

Status

The application is fully functional and ready to be enjoyed as is. Future updates and improvements are still a possibility.

Contact

Created by Bryce Kennedy

If you have any questions or comments feel free to reach out to me and thank you for your time.

License

Click to view

quizard's People

Contributors

btken88 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.