-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath-flash-card.rb
More file actions
99 lines (92 loc) · 2.03 KB
/
math-flash-card.rb
File metadata and controls
99 lines (92 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Methods
# Game starter
def start_game
puts "Welcome to Math Flashcards!"
puts "Select your game type (add|subtract|multiply|divide)"
game_type = gets.chomp.downcase
if game_type == "add"
math_method = "add"
math_flashcards(
math_method: math_method,
)
elsif game_type == "subtract"
math_method = "subtract"
math_flashcards(
math_method: math_method,
)
elsif game_type == "multiply"
math_method = "multiply"
math_flashcards(
math_method: math_method,
)
elsif game_type == "divide"
math_method = "divide"
math_flashcards(
math_method: math_method,
)
else
puts "Sorry I didn't recognize. Please hit enter to try again"
gets
start_game
end
end
def math_flashcards(math_method:)
first_number = Random.rand(0..10)
second_number = Random.rand(0..10)
system "clear"
# Division Safety Check
if second_number == 0
math_flashcards(math_method: math_method,)
elsif math_method == "divide" && first_number % second_number > 0
math_flashcards(math_method: math_method,)
else
end
puts "What does #{first_number} #{math_method} #{second_number} equal?"
user_answer = gets.chomp.to_i
if math_method == "subtract"
if user_answer == first_number - second_number
puts "Success! Try again?"
gets
start_game
else
puts "Sorry but incorrect. Press enter to try again."
gets
start_game
end
end
if math_method == "multiply"
if user_answer == first_number * second_number
puts "Success! Try again?"
gets
start_game
else
puts "Sorry but incorrect. Press enter to try again."
gets
start_game
end
end
if math_method == "divide"
if user_answer == first_number / second_number
puts "Success! Try again?"
gets
start_game
else
puts "Sorry but incorrect. Press enter to try again."
gets
start_game
end
end
if math_method == "add"
if user_answer == first_number + second_number
puts "Success! Try again?"
gets
start_game
else
puts "Sorry but incorrect. Press enter to try again."
gets
start_game
end
end
end
# Method starter
start_game