-
Notifications
You must be signed in to change notification settings - Fork 52
Leaves - Nicky #51
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?
Leaves - Nicky #51
Conversation
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.
Your code is clear and makes good use of new concepts (methods, case/when). You have several clever solutions to dealing with invalid user input. See code review for a few minor suggestions on how to make your code more concise.
|
||
# Checks and validates user input. If input is not an integer, an exception is thrown and prompts the user to enter a valid number | ||
begin | ||
num_one = Integer(gets.chomp) # Question: Is there a way to accept both integers and floats from the user? Would this require conditional statements? |
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.
Instead of Interger(gets.chomp) you could use Float(gets.chomp). Converting to Floats is a better option so that it performs division correctly (5/2 = 2.5 (as opposed to 2).)
|
||
puts "Please enter another number:" | ||
|
||
begin |
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.
Consider using a loop to DRY up your code.
retry | ||
end | ||
|
||
# Performs calculations based on user input |
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.
The is a clever use of begin/rescue/retry. Nice research!
end | ||
|
||
# Performs calculations based on user input | ||
if users_operator == "ADD" || users_operator == "+" |
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.
Consider using a case/when block to simplify your code.
puts "Please choose one operator(name or symbol):" | ||
|
||
# Stores user's input as one of the operators or prints a message telling them to enter a valid operator | ||
while users_operator = gets.chomp.upcase |
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 clever -- however an includes?
method would simplify this operation:
until [array of valid operations].includes?(users_operator)
CalculatorWhat We're Looking For
|
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions
What I thought was lacking was for the variable names within the methods, I thought it may be difficult to differentiate between the two variable names (num_one and num_two) especially when they're all the same amongst all the calculation methods.
For user's number inputs, I used begin/rescue/retry statements which will throw an exception if a user does not enter a valid integer input. I was not able to also check for a float input so I was wondering if that would require additional conditional statements?