Skip to content
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
83 changes: 83 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
puts "Welcome to the Ruby Calculator!"

operator = ""
# valid operators
operators = [
"add", "+", "1",
"subtract", "-", "2",
"multiply", "*", "3",
"divide", "/", "4",
"exponent", "**", "5",
"modulo", "%", "6"
]

# prompt user for operator until they enter a valid option
until operators.include?(operator)
puts "Please choose an operation from the list (name, symbol, or number):
1. add(+)
2. subtract(-)
3. multiply(*)
4. divide(/)
5. exponent(**)
6. modulo(%)"
operator = gets.chomp.to_s.downcase
end

# update operator to operation to use in user prompt
case operator
when "add", "+", "1"

Choose a reason for hiding this comment

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

The when should be indented under the case.

operator = "addition"
when "subtract", "-", "2"
operator = "subtraction"
when "multiply", "*", "3"
operator = "multiplication"
when "divide", "/", "4"
operator = "division"
when "exponent", "**", "5"
operator = "exponent"
when "modulo", "%", "6"
operator = "modulo"
end

# validate that user input is numeric
def validate_number(num)

Choose a reason for hiding this comment

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

I suggest moving your methods to the top of the file for now, later another file, and putting the main driver code below.

while num.to_i.to_s != num.strip
puts "#{num} is not a valid number! Enter a new number: "
num = gets.chomp
end
return num
end

# prompt user for number inputs
puts "You chose #{operator}! Enter your first number: "
num1 = gets.chomp
num1 = validate_number(num1)

puts "Enter your second number: "
num2 = gets.chomp
num2 = validate_number(num2)

# display result based on operator
case operator
when "addition"
puts "#{num1} + #{num2} = #{num1.to_i + num2.to_i}"
when "subtraction"
puts "#{num1} - #{num2} = #{num1.to_i - num2.to_i}"
when "multiplication"
puts "#{num1} * #{num2} = #{num1.to_i * num2.to_i}"
when "division"
# prevent user from entering 0 for num2 during division
if num2 == "0"
puts "You cannot divide by 0!"
else
puts "#{num1} / #{num2} = #{num1.to_i / num2.to_i}"
end
when "exponent"
formula = ""
num2.to_i.times do |i|
formula += "#{num1} * "
end
puts "#{formula.chomp(" * ")} = #{num1.to_i ** num2.to_i}"
when "modulo"

Choose a reason for hiding this comment

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

You should also check for divide by 0 when using modulo.

puts "#{num1} % #{num2} = #{num1.to_i % num2.to_i}"
end