Commit a87fc111 authored by Owen Ilagan's avatar Owen Ilagan

Update Quiz

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