-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DaisyMolving
wants to merge
6
commits into
master
Choose a base branch
from
refactoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactoring #1
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8ee010
rewrote readme for escript running with mix
DaisyMolving 1107023
edited mix.exs to add escript and run
DaisyMolving 185a9e2
extracted actions for correct input into method called correct_input_…
DaisyMolving c23e274
added more questions so now there are ten, added more inputs to tests…
DaisyMolving d254b4f
changed name of main module in escript to be correct, reformatted que…
DaisyMolving e20f4e7
added private function to introduce quiz, called before questions are…
DaisyMolving File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" }, | ||
| %{ 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]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expand this pipe 😄 |
||
| |> validate_input(current_question[:answer], score, next_questions) | ||
| end | ||
|
|
||
| def request_answer(question) do | ||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😄👍👏
There was a problem hiding this comment.
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: