-
Notifications
You must be signed in to change notification settings - Fork 48
Naheed - Calculator - Edges #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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 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.
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 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