Skip to content

Conversation

@elle-terch
Copy link

Calculator

Congratulations! You're submitting your assignment.

Comprehension Questions

Question Answer
Describe how you stored user input in your program? I stored user input in my program using local variables number_1 and number_2 (for values) and operator.
How did you determine what operation to perform? I used a case statement to evaluate the user input as either a symbol or the name of the operator.
Do you feel like you used consistent indentation throughout your code? I think so?
If you had more time, what would you have added to or changed about the program? I would have refactored the code block (that accepted values) into a method or a times loop. I also would have added a loop around the case statement so if the user entered something invalid, they would have the opportunity to try again.

@droberts-sea
Copy link

Calculator

What We're Looking For

Feature Feedback
Takes in two numbers and an operator and performs the mathematical operation. yes
Readable code with consistent indentation. yes

Great job overall! This program works well, and I appreciate the way you used whitespace to break up your code. There were a few things that could be cleaned up that I've tried to call out below, but in general I am quite happy with this submission. Keep up the hard work!

begin
number_2 = gets.chomp
number_2 = Float(number_2)
rescue

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?

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

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.

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

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.

case operator
when "add", "+"
add_two_numbers(number_1, number_2)
when "subtract", "-"

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants