-
Notifications
You must be signed in to change notification settings - Fork 51
Create calculator.rb #53
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
beccaelenzil
left a comment
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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