-
Notifications
You must be signed in to change notification settings - Fork 48
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
BarbaraWidjono, Edges, calculator.rb #41
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Welcome message for user | ||
puts "Welcome to the Ada Calculator!" | ||
puts "These are the following choices:" | ||
puts "1. add(+)" | ||
puts "2. subtract(-)" | ||
puts "3. multiply(*)" | ||
puts "4. divide(/)" | ||
puts "Please choose an operand:" | ||
|
||
# Accepting user choice (case insensitive) | ||
operand = gets.chomp.downcase | ||
|
||
# Ensuring user is choosing a valid operation | ||
until ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"].include? (operand) do | ||
puts "Please enter a valid operand" | ||
operand = gets.chomp.downcase | ||
end | ||
|
||
# Asking user to input values to be used in calculations | ||
puts "Please enter the first value: " | ||
first_value = gets.chomp.to_f | ||
|
||
# Insert logic to ensure first_value entered is a number | ||
|
||
puts "Please enter the second value: " | ||
second_value = gets.chomp.to_f | ||
|
||
# Insert logic to ensure second_value entered is a number | ||
|
||
# Defining methods for each operand | ||
def addittion(x, y) | ||
return (x + y) | ||
end | ||
|
||
def subtraction(x, y) | ||
return (x - y).to_f | ||
end | ||
|
||
def multiplication(x, y) | ||
return (x * y).to_f | ||
end | ||
|
||
def division (x , y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though the code still runs, most style guides, style conventions, and text editors like Atom will complain/give a warning if there's a space between the method name and the parentheses. aka: def division (x , y) should be this: def division(x , y) |
||
# Insert conditional: If y == 0, then print message "Cannot divide by zero" and enter a while loop that states while y == 0, keep asking user to input a valid value. When y != 0, return (x / y). Else, return (x / y) | ||
return (x / y).to_f | ||
end | ||
|
||
# Determining which method to invoke depending on the operand chosen by the user | ||
if operand == "add" || operand == "+" | ||
puts "Answer: #{first_value} + #{second_value} = #{addittion(first_value, second_value)}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like how concise these lines are: You are interpolating things and calling methods in them -- it shows that you understand that many method calls can happen on one line, which is a crucial piece of understanding! |
||
elsif operand == "subtract" || operand == "-" | ||
puts "Answer: #{first_value} - #{second_value} = #{subtraction(first_value, second_value)}" | ||
elsif operand == "multiply" || operand == "*" | ||
puts "Answer: #{first_value} * #{second_value} = #{multiplication(first_value, second_value)}" | ||
elsif operand == "divide" || operand == "/" | ||
puts "Answer: #{first_value} / #{second_value} = #{division(first_value, second_value)}" | ||
end |
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 end up calling
.to_f
on this result (and in some other calculation methods).However, let's establish some things:
first_value
andsecond_value
by getting user input and then converting them to floats immediately with.to_f
.And let's remember:
Therefore: do you need to convert to a float here?
Sometimes, it's good to have a
.to_f
in here just in case there's a chance you still need to convert it, but within this particular program, you probably wouldn't need to call.to_f
here. Not a big deal! Just wanted to call out this redundancy.