Conversation
beccaelenzil
left a comment
There was a problem hiding this comment.
Your code is clear and well organized. You incorporated new concepts well (until, case/when). In the future be sure to test your code for edge cases (such as invalid user input) so that you can catch small bugs. You commented out validation of the first number which creates a bug when a user enters invalid input for this number. See the line by line code review for additional small suggestions.
| Please choose an operator (name or symbol)" | ||
| operator = gets.chomp.downcase.to_s | ||
|
|
||
| until operator == "add" || operator == "+" || operator == "subtract" || operator == "-" || operator == "multiply" || operator == "*" || operator == "divide" || operator == "/" || operator == "exponents" || operator == "**" || operator == "modulo" || operator == "%" |
There was a problem hiding this comment.
consider using the method inlclude? here to simplify your code, for example:
until [array of valid operators].include?(operator)| operator = gets.chomp.downcase.to_s | ||
| end | ||
|
|
||
| puts "Enter the first number: " |
There was a problem hiding this comment.
Consider using a loop here to DRY up your code. Also, your validation for the first number is commented out, and as such that program doesn't appropriately deal with invalid user input for the first number.
| num_two = gets.chomp | ||
| end | ||
|
|
||
| num_one = num_one.to_i |
There was a problem hiding this comment.
You should convert your numbers to floats rather than ints so that the divide operator gives you the correct results. For example 5/6 should not equal 0.
| num_one = num_one.to_i | ||
| num_two = num_two.to_i | ||
|
|
||
| if operator == "divide" || operator == "/" && num_one == 0 || num_two == 0 |
There was a problem hiding this comment.
It is ok for num_one to equal 0. 0/5 = 0, where as 5/0 is invalid.
| puts "Great! #{num_one} % #{num_two} = #{answer}" | ||
| end | ||
|
|
||
| case operator |
There was a problem hiding this comment.
Nice use of case/when to simplify your code. Minor note: you should tab in each line of code under when.
| end | ||
| end | ||
|
|
||
| def addition(num_one, num_two) |
There was a problem hiding this comment.
Good use of methods to encapsulate functionality of each operation. Consider returning the answer in each of these methods in case you want to use it elsewhere.
CalculatorWhat We're Looking For
|
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions