Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
# TriviaApp
To Run This App:

**TODO: Add description**
Install Elixir

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:

1. Add `trivia_app` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:trivia_app, "~> 0.1.0"}]
end
```

2. Ensure `trivia_app` is started before your application:

```elixir
def application do
[applications: [:trivia_app]]
end
```
Run `mix escript.build`

Run `./quiz_master`
50 changes: 41 additions & 9 deletions lib/trivia_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ defmodule TriviaApp do
end

@questions [
%{ question: "Samite is a type of what: a) Fabric? b) Stone? c) Dog? d) Cake?", answer: "a" },
%{ question: "Vermillion is a shade of which colour: a) Green? b) Blue? c) Red? d) Yellow?", answer: "c" }
%{ question: "Samite is a type of what: \na) Fabric \nb) Stone \nc) Dog \nd) Cake ", answer: "a" },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty cool ☺️
The next stage in "extraction" is to figure out a file format, so you don't even need to re-build the application to add new questions 😄👍👏

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Felipe!
Is figuring out a file format when you split the responsibilities into
seperate modules?

On Mon, Sep 5, 2016 at 4:21 PM, Felipe Seré notifications@github.com
wrote:

In lib/trivia_app.ex
#1 (comment)
:

@@ -5,18 +5,28 @@ defmodule TriviaApp do
end

@Questions [

  •            %{ question: "Samite is a type of what: a) Fabric? b) Stone? c) Dog? d) Cake?", answer: "a" },
    
  •            %{ question:  "Vermillion is a shade of which colour: a) Green? b) Blue? c) Red? d) Yellow?", answer: "c" }
    
  •            %{ question: "Samite is a type of what: \na) Fabric \nb) Stone \nc) Dog \nd) Cake ", answer: "a" },
    

This is pretty cool ☺️
The next stage in "extraction" is to figure out a file format, so you
don't even need to re-build the application to add new questions 😄👍👏


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/DaisyMolving/quiz_master_elixir/pull/1/files/e20f4e75dd8bd4e797b0938517e08a2be2aaef45#r77536015,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALbNsYMuqsl-QxtgJVLLfBCGDqtRw6d9ks5qnDNtgaJpZM4J1Gmb
.

%{ question: "Vermillion is a shade of which colour: \na) Green \nb) Blue \nc) Red \nd) Yellow ", answer: "c" },
%{ question: "An anemometer is a gauge used for recording the speed of what: \na) Light \nb) Spacecraft \nc) Wind \nd) Athletes ", answer: "c"},
%{ question: "English novelist William Godwin fathered which novelist daughter: \na) George Eliot \nb) Mary Shelley \nc) Emily Brontë \nd) Jane Austen ", answer: "b"},
%{ question: "Scurvy is a deficiency in what: \na) Vitamin A \nb) Vitamin B \nc) Vitamin C \nd) Vitamin D ", answer: "c"},
%{ question: "What does an ornithologist study? \na) Diseases \nb) Ancient Pottery \nc) Birds \nd) Teeth ", answer: "c"},
%{ question: "What is the scientific name for red blood cells? \na) Erythrocytes \nb) Leukocytes \nc) Thrombocytes \nd) Keratinocytes ", answer: "a"},
%{ question: "What are the names of Mars’ two satellites? \na) Pallas and Vesta \nb) Phobos and Deimos \nc) Ganymede and Callisto \nd) Triton and Nereid ", answer: "b"},
%{ question: "What is the atomic number for Lithium? \na) 7 \nb) 18 \nc) 3 \nd) 24 ", answer: "c"},
%{ question: "Asteroids that orbit the sun without crossing Earth’s orbit is are called: \na) Amor \nb) Aten \nc) Apollo \nd) Icarus ", answer: "a"}
]

def start_quiz do
introduce_quiz
score = 0
ask_question(@questions, score)
end

def ask_question([], _), do: perfect_score_message
def ask_question([current_question | next_questions], score) do
validate_input(request_answer(current_question[:question]), current_question[:answer], score, next_questions)
request_answer(current_question[:question])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expand this pipe 😄
current_question[:question]
|> request_answer
|> validate_answer...
|> continue?

|> validate_input(current_question[:answer], score, next_questions)
end

def request_answer(question) do
Expand All @@ -26,11 +36,29 @@ defmodule TriviaApp do
def validate_input(user_input, correct_answer, score, next_questions) do
cond do
String.strip(user_input) == correct_answer ->
print_correct_answer_message
print_score(score + 1)
ask_question(next_questions, score + 1)
correct_input_action(score, next_questions)
:else ->
print_incorrect_answer_message
incorrect_input_action
end
end

defp introduce_quiz do
IO.puts("Welcome to the quiz! Here are your Questions: ")
end

defp correct_input_action(score, next_questions) do
print_correct_answer_message
print_score(score + 1)
ask_question(next_questions, score + 1)
end

defp incorrect_input_action do
print_incorrect_answer_message
cond do
String.strip(start_over_message) == "y" ->
start_quiz
:else ->
IO.puts("Goodbye!")
end
end

Expand All @@ -43,11 +71,15 @@ defmodule TriviaApp do
end

defp print_incorrect_answer_message do
IO.puts("OOPS! That answer is incorrect!")
IO.puts("OOPS! That answer is incorrect! ")
end

defp start_over_message do
IO.gets("Would you like to start again? Type y for yes, or n for no: ")
end

defp perfect_score_message do
IO.puts("Congratulations! You have finished the quiz with a perfect score")
IO.puts("Congratulations! You have finished the quiz with a perfect score!")
end

end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule TriviaApp.Mixfile do
[app: :trivia_app,
version: "0.1.0",
elixir: "~> 1.3",
escript: [main_module: TriviaApp],
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
Expand Down
10 changes: 8 additions & 2 deletions test/trivia_app_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule TriviaAppTest do
end

test "validates user input by checking it against incorrect answer" do
assert capture_io(fn ->
assert capture_io("b\na", fn ->
TriviaApp.validate_input("a", "b", 0, [%{question: "huh?", answer: "a"}])
end) =~ "incorrect"
end
Expand Down Expand Up @@ -42,7 +42,13 @@ defmodule TriviaAppTest do
end

test "given correct answers only, the game ends with a perfect score" do
assert capture_io("a\nc", fn ->
assert capture_io("a\nc\nc\nb\nc\nc\na\nb\nc\na", fn ->
TriviaApp.start_quiz
end) =~ "perfect score"
end

test "given incorrect answers, but also given the order to start again, quiz starts again" do
assert capture_io("b\ny\na\nc\nc\nb\nc\nc\na\nb\nc\na", fn ->
TriviaApp.start_quiz
end) =~ "perfect score"
end
Expand Down