Conversation
CalculatorWhat We're Looking For
Good job overall! This code works well, and you've done a good job of using whitespace to break up the code and make it more readable. There are a few places where you could have used methods to DRY up the code or make it more readable, which I've tried to call out below, but in general I am quite happy with this submission. Keep up the hard work! |
| # Ask user for numerical value input and validate input to be an integer | ||
| puts "Please input your numbers" | ||
| puts "First number: " | ||
| first_num = gets.chomp |
| puts "Second number: " | ||
| second_num = gets.chomp | ||
| until !((second_num !='0') && (second_num.to_i.to_s != second_num)) | ||
| puts "Please enter a valid number" |
There was a problem hiding this comment.
You've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method?
| if operation == "add" || operation == "+" | ||
| puts "#{first_num} + #{second_num} = #{addition(first_num, second_num)}" | ||
| elsif operation == "subtract" || operation == "-" | ||
| puts "#{first_num} - #{second_num} = #{subtraction(first_num, second_num)}" |
There was a problem hiding this comment.
This code isn't repeated, but I think it would still increase readability to wrap this if/elseif 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(operation, first_num, second_num).
| attempt = 0 | ||
| while !(operation == "add" || operation == "+" || operation == "subtract" || operation == "-" || operation == "multiply" || operation == "*" || operation == "divide" || operation == "/") | ||
| if attempt < 3 | ||
| puts "Oops your input was not recognized. Please try again " |
There was a problem hiding this comment.
This part where you get an operation from the user is another place where it would be good for readability to wrap it in a method.
| if second_num == 0 || second_num == nil | ||
| puts "You can not divide any number by zero or nil." | ||
| else | ||
| puts "#{first_num} / #{second_num} = #{division(first_num, second_num)}" |
There was a problem hiding this comment.
Why not put this logic to detect zero division inside the division method?
| # additions | ||
| def addition(num1, num2) | ||
| return num1 + num2 | ||
| end |
There was a problem hiding this comment.
I like that you've made each of these a separate method
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions