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
66 changes: 66 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# method defining which operator user chooses
def operatorchoice(operator)

Choose a reason for hiding this comment

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

Suggested rename

Suggested change
def operatorchoice(operator)
def operator_choice(operator)

case operator
when "add", "+", "addition", "adding", "add(+)"

Choose a reason for hiding this comment

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

You should indent the when blocks over case.

return "+"
when "subtract", "-", "subtraction", "subtracting", "subtract(-)"
return "-"
when "multiply", "*", "multiplication", "multiplying", "multiply(*)"
return "*"
when "divide", "/", "division", "dividing", "divide(/)"
return "/"
else
return "invalid"
end
end


# introduction to user and prompt choice of operator
puts "Welcome to the Calculator! Which operator would you like to use?
add(+)
subtract(-)
multiply(*)
divide(/)
Please choose one operator(name OR symbol):"

operator_entered = gets.chomp
operator_active = operatorchoice(operator_entered)
while operator_active == "invalid"
puts "That is not a valid operator. Please choose again one operator(name or symbol):"
operator_entered = gets.chomp
operator_active = operatorchoice(operator_entered)
end

puts "Please enter number 1: "
ans1 = gets.chomp
num1 = ans1.to_f
until ans1.to_i.to_s == ans1 || ans1.to_f.to_s == ans1
puts "Please re-enter number 1: "
ans1 = gets.chomp
num1 = ans1.to_f
end
Comment on lines +35 to +41

Choose a reason for hiding this comment

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

Why is this indented?


puts "Please enter number 2"
ans2 = gets.chomp
num2 = ans2.to_f
until ans2.to_i.to_s == ans2 || ans2.to_f.to_s == ans2
puts "Please re-enter number 2: "
ans2 = gets.chomp
num2 = ans2.to_f
end

# defining method for calculation
def calculator(operator_active, num1, num2)
case operator_active
when "+"
return num1 + num2
when "-"
return num1 - num2
when "*"
return num1 * num2
when "/"
return num1 / num2
end
end

puts "Answer: #{num1} #{operator_active} #{num2} = #{calculator(operator_active, num1, num2)}"