Skip to content

Conversation

@morganschuler
Copy link

Calculator

Congratulations! You're submitting your assignment.

Comprehension Questions

Question Answer
What went well in your code style, such as indentation, spacing, variable names, readability, etc.? What was lacking? I think my variable names and use of loops was accurate in my code. I think my use of loops could be improved in the valid input variable.
How did your code keep track of user input? My code used a case/when statement with regex. If the user entered a character that was not a numerical or inserted an empty string, they were prompted again for valid input.
How did your code determine what operation to perform? My code used an until loop to ensure the user entered the correct operator.
What opportunities exist to create small methods for this project? I used methods to return the mathematical equations based on the operator that the user chose.
In the next project, what would you change about your process? What would you keep doing?
In the event that a user inputs invalid input when getting the first or second number, they are told that it is invalid and prompted to insert another number. However, if they enter invalid input again after this, the program keeps moving forward. I wanted to install a type of "until" loop that would continue to prompt the user until they wrote valid input, but I was unsure of how to do this with, and this is something that I think could be improved in my code. Aside from this, I think

Copy link

@beccaelenzil beccaelenzil left a 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 == "%"

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: "

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

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

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

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)

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.

@beccaelenzil
Copy link

Calculator

What We're Looking For

Feature Feedback
Readable code with consistent indentation yes
Practices using variables appropriately yes
Practices using conditionals appropriately yes
If any, practices iteration appropriately yes
If any, practices using custom methods appropriately yes
Takes in two numbers and an operator and can perform addition yes
Takes in two numbers and an operator and can perform subtraction yes
The program handles divide when attempting to divide by zero yes, but incorrectly indicates 0/n as invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants