Skip to content

Instantly share code, notes, and snippets.

@ryanckulp
Last active January 1, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanckulp/70ab24ff63b9542951288ec977a5a62c to your computer and use it in GitHub Desktop.
Save ryanckulp/70ab24ff63b9542951288ec977a5a62c to your computer and use it in GitHub Desktop.
a sample flash card application in Ruby
questions = {
'one' => {text: "What is 2 + 2?", answer: 4},
'two' => {text: "What is 4 + 7?", answer: 11},
'three' => {text: "What is 7 + 3?", answer: 10},
}
user_answers = {
'one' => 4,
'two' => 9,
'three' => 10
}
def addition_problem(user_answer, correct_answer)
if user_answer == correct_answer
true
else
false
end
end
total_correct = 0
user_answers.each do |question_number, user_answer|
correct_answer = questions[question_number][:answer]
if addition_problem(user_answer, correct_answer) # => will return true or false
total_correct +=1
end
end
puts "You scored #{total_correct} out of #{user_answers.count} correct!"
# => You scored 2 out of 3 correct!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment