Locked lesson.
About this lesson
How to handle subtraction, multiplication and division flashcards.
Exercise files
Download this lesson’s related exercise files.
Flashcard Subtraction, Multiplication, Division Methods.docx59.2 KB Flashcard Subtraction, Multiplication, Division Methods - Solution.docx
59.8 KB
Quick reference
Flashcard Subtraction, Multiplication, Division Methods
Let's build out the rest of the math methods for our app.
When to use
Do this once you've finished your addition method.
Instructions
You can copy and paste your addition method, just change the math operator for each method:
# Subtraction Method!!
def subtract
rand_1 = Random.rand(0..10)
rand_2 = Random.rand(0..10)
correct_answer = rand_1 - rand_2
system "clear"
print "What is #{rand_1} - #{rand_2}: "
answer = gets.to_i
if answer == correct_answer
system "clear"
puts "Correct! #{rand_1} - #{rand_2} = #{correct_answer}!!\n\n"
else
system "clear"
puts "Wrong!! #{rand_1} - #{rand_2} = #{correct_answer}, not #{answer}!!\n\n"
end
end
# Multiplication Method
def multiply
rand_1 = Random.rand(0..10)
rand_2 = Random.rand(0..10)
correct_answer = rand_1 * rand_2
system "clear"
print "What is #{rand_1} * #{rand_2}: "
answer = gets.to_i
if answer == correct_answer
system "clear"
puts "Correct! #{rand_1} * #{rand_2} = #{correct_answer}!!\n\n"
else
system "clear"
puts "Wrong!! #{rand_1} * #{rand_2} = #{correct_answer}, not #{answer}!!\n\n"
end
end
# Division Method
def divide
rand_1 = Random.rand(0..10)
rand_2 = Random.rand(0..10)
correct_answer = rand_1 / rand_2
system "clear"
print "What is #{rand_1} / #{rand_2}: "
answer = gets.to_i
if answer == correct_answer
system "clear"
puts "Correct! #{rand_1} / #{rand_2} = #{correct_answer}!!\n\n"
else
system "clear"
puts "Wrong!! #{rand_1} / #{rand_2} = #{correct_answer}, not #{answer}!!\n\n"
end
end
Hints & tips
- Copy and paste your addition method for each of the other math methods, just change the math operator in each one
Lesson notes are only available for subscribers.