Naheed - Calculator - Edges#32
Conversation
CalculatorWhat We're Looking For
You didn't use methods at all in this implementation. The code works the way it's supposed to, but working with methods is one of the core learning goals of this assignment, and I'm worried that you missed this opportunity to practice them. As you work on the data transformation worksheet and RideShare, please make sure you're wrapping each piece of work in its own method. Other than that, this submission looks good. There are a few places where things could be cleaned up that I've tried to outline below, but in general I'm happy with the code you've written. Keep up the hard work! |
| #gets numbers from user | ||
| puts "First number:" | ||
| first_number = gets.chomp.to_f | ||
| puts "Second number:" |
There was a problem hiding this comment.
Watch your indentation. I don't think these lines should be indented at all.
|
|
||
| #checking if number is integer by dividing it by itself | ||
| if first_number / first_number == 1 && second_number / second_number == 1 | ||
|
|
There was a problem hiding this comment.
This is a creative way to address this problem. Unfortunately it's got one big issue: it doesn't work for 0!
| puts "#{first_number} * #{second_number} = #{first_number * second_number}" | ||
| elsif chosen_operator == "divide" || chosen_operator == "/" | ||
| puts "#{first_number} / #{second_number} = #{first_number / second_number}" | ||
| end |
There was a problem hiding this comment.
You should have an else clause here so that when the user inputs an invalid operator, you can give them a helpful message about what went wrong. As is your program fails silently.
|
|
||
| #gets numbers from user | ||
| puts "First number:" | ||
| first_number = gets.chomp.to_f |
There was a problem hiding this comment.
You've written almost exactly the same code twice to get the two numbers from the user. As is it's only two lines, so it's not so bad, but it makes it difficult to expand that logic. For example, you might want to run this code in a loop until the user enters a valid number. Doing this in a method would mean you only have to write that code once.
| if chosen_operator == "add" || chosen_operator == "+" | ||
| puts "#{first_number} + #{second_number} = #{first_number + second_number}" | ||
| elsif chosen_operator == "subtract" || chosen_operator == "-" | ||
| puts "#{first_number} - #{second_number} = #{first_number - second_number}" |
There was a problem hiding this comment.
This code isn't repeated, but I think it would still increase readability to wrap this if/elsif 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(chosen_operator, first_number, second_number).
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions