Commit a87fc111 authored by Owen Ilagan's avatar Owen Ilagan

Update Quiz

parent fdcf6b76
......@@ -45,16 +45,13 @@ class WelcomeController < ApplicationController
def quiz
@score = params[:score]
if @score.nil? || @score.empty?
@score = '0'
@question = '0'
end
def quiz_answer
@score = params[:score]
@question = params[:question]
if !@question.nil?
if @question.empty?
@question = '0'
end
if @question.to_i > 0
correct_answer = getQuestions[@question.to_i-1][1]
......@@ -66,18 +63,16 @@ class WelcomeController < ApplicationController
length = getQuestions.size
if @question.to_i == length
@question_text = 'End of the quiz. You scored ' + @score + ' out of ' + length.to_s
@results = 'End of the quiz. You scored ' + @score + ' out of ' + length.to_s
@question = '0'
@score = nil
@score = '0'
else
@question_text = getQuestions[@question.to_i][0]
@answers = getQuestions[@question.to_i][2]
@question = (@question.to_i + 1).to_s
end
else
@question = ''
end
render 'quiz'
end
private
......
<div class="jumbotron">
<div class="container">
<h1>Take a Quiz!</h1>
<% if @question.empty? %>
<p>Test your general knowledge! Click START to begin the quiz.</p>
<%if !@results.nil? %>
<h2><%=@results%></h2>
<% else %>
<%if @question == '0'%>
<h2><%=@question_text%></h2>
<% if @question == '0' %>
<p>Test your general knowledge! Click START to begin the quiz.</p>
<% else %>
<h2>Question #<%=@question%></h2>
<p><%=@question_text%></p>
......@@ -15,11 +15,9 @@
<%= form_tag("/quiz", method: "post") do %>
<%= hidden_field( nil, :question, value: @question)%>
<%= hidden_field( nil, :score, value: @score)%>
<% if @question.empty? %>
<% if @question == '0' %>
<input class="btn btn-primary btn-lg" type="submit" value="Start »"></input>
<% else %>
<%if @question != '0'%>
<ul class="list-group">
<% @answers.each_with_index do |item, index| %>
<li class="list-group-item">
......@@ -28,9 +26,8 @@
</li>
<% end %>
</ul>
<% end %>
<%if @question == '0'%>
<%if !@results.nil? %>
<input class="btn btn-primary btn-lg" type="submit" value="Try Again »"></input>
<% else %>
<input class="btn btn-primary btn-lg" type="submit" value="Answer »"></input>
......
......@@ -11,7 +11,7 @@ Rails.application.routes.draw do
get 'random' => 'welcome#random'
post 'random' => 'welcome#random'
get 'quiz' => 'welcome#quiz'
post 'quiz' => 'welcome#quiz'
post 'quiz' => 'welcome#quiz_answer'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment