Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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