Conversation
beccaelenzil
left a comment
There was a problem hiding this comment.
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.
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.
Consider using a loop to DRY up your code.
| retry | ||
| end | ||
|
|
||
| # Performs calculations based on user input |
There was a problem hiding this comment.
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.
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.
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?