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
63 changes: 63 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#Welcome user and accept numbers to be evaluated. Validate user input.
puts "Welcome to the calculator program!"
puts "You will need to enter two numbers to be evaluated."
puts "Please enter the first."
begin
number_1 = gets.chomp
number_1 = Float(number_1)
rescue
puts "There is an error with your number. Please make sure you're using numbers/decimals only and try again."
retry
end

puts "Please enter another number."
begin
number_2 = gets.chomp
number_2 = Float(number_2)
rescue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method?

puts "There is an error with your number. Please make sure you're using numbers/decimals only and try again."
retry
end

#create a method for each operator
def add_two_numbers(first_number, second_number)
puts "#{first_number} + #{second_number} = #{first_number + second_number}"
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These methods all puts things. That ends up solving the problem, but in general you want to be in the habit of returning things from methods, and then having whoever called the method do the putsing.


def subtract_two_numbers(first_number, second_number)
puts "#{first_number} - #{second_number} = #{first_number - second_number}"
end

def multiply_two_numbers(first_number, second_number)
puts "#{first_number} * #{second_number} = #{first_number * second_number}"
end

def divide_two_numbers(first_number, second_number)
if second_number == 0
puts "#{first_number} / #{second_number} = According to Wikipedia, the result of any number divided by zero is undefined."
else

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like that you've included the division-specific error handling code in the division method. This feels like good organization to me.

puts "#{first_number} / #{second_number} = #{first_number / second_number}"
end
end

#prompt the user to choose their operator
puts "Choose your operator! Type the name or symbol from the list below:"
puts "1. add (+)"
puts "2. subtract (-)"
puts "3. multiply (*)"
puts "4. divide (/)"
operator = gets.chomp

#use the appropriate method to evaluate the values
case operator
when "add", "+"
add_two_numbers(number_1, number_2)
when "subtract", "-"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This code isn't repeated, but I think it would still increase readability to wrap this case/when section in a method. That would clearly delineate where it starts and ends, and make it explicit what data it needs to work. The method signature might be something like perform_calculation(op, first, second).

subtract_two_numbers(number_1, number_2)
when "multiply", "*"
multiply_two_numbers(number_1, number_2)
when "divide", "/"
divide_two_numbers(number_1, number_2)
else
puts "I'm sorry. That is not a valid option."
end