Skip to content

Branches - Paige #31

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Calculator Program

# stores valid input options for math operations
valid_maths = [
"add",
"+",
"subtract",
"-",
"multiply",
"*",
"divide",
"/",
"exponent",
"^",
"modulo",
"%"
]

# method for text displaying available math options
def maths_text
puts " Add (+)"
puts " Subtract (-)"
puts " Multiply (*)"
puts " Divide (/)"
puts " Exponent (^)"
puts " Modulo (%)"
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that this display functionality is pulled into a method! Keep doing things like this! Also, I like my method names to usually be actions or action-like. Consider maybe changing this method name to display_maths_text or show_maths_text?


# displays welcome message
puts "Welcome to the Calculator Program!"

# displays available math operations
# asks user to input a math operation
puts "Please select one of the following options: "
maths_text
print "Selection: "
operation = gets.chomp.downcase

until valid_maths.include?(operation)
puts "I can't do that. I only know how to:"
maths_text
print "Which operation do you want me to perform? "
operation = gets.chomp.downcase
end

# asks user to provide a number
print "First number: "
num_1 = gets.chomp
until num_1.to_i.to_s == num_1 || num_1.to_f.to_s == num_1
puts "That's not a number. Try again."
print "First number: "
num_1 = gets.chomp
end

# asks user to provide a second number
print "Second number: "
num_2 = gets.chomp
until num_2.to_i.to_s == num_2 || num_2.to_f.to_s == num_2
puts "That's not a number. Try again."
print "Second number: "
num_2 = gets.chomp
end

# asks user to state if result should be integer or float
print "Do you want the result as an integer? "
result_integer = gets.chomp.downcase

case result_integer
when "yes", "y"
num_1 = num_1.to_i
num_2 = num_2.to_i
when "no", "n"
num_1 = num_1.to_f
num_2 = num_2.to_f
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!


# executes and prints math operation
puts "\n--CALCULATION--"
case operation
when "add", "+"
result = num_1 + num_2
puts "#{num_1} + #{num_2} = #{result}"
when "subtract", "-"
result = num_1 - num_2
puts "#{num_1} - #{num_2} = #{result}"
when "multiply", "*"
result = num_1 * num_2
puts "#{num_1} * #{num_2} = #{result}"
when "divide", "/"
if num_2 == 0
puts "ERROR -- You can't divide by 0!"
else
result = num_1 / num_2
puts "#{num_1} / #{num_2} = #{result}"
end
when "exponent", "^"
result = num_1 ** num_2.to_i
puts "#{num_1} ^ #{num_2} = #{result}"
when "modulo", "%"
result = num_1 % num_2
puts "#{num_1} % #{num_2} = #{result}"
end