Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Calculator Project - 8.8.18

# print welcome message and operator options
puts "Welcome to The Calculator! Which operator would you like to use?"
puts "1. add(+)"
puts "2. subtract(-)"
puts "3. multiply(*)"
puts "4. divide(/)"

# user input - operation
puts "Please choose one operator. You can enter the name or the symbol."
op = gets.chomp

valid_ops = %w[add + subtract - multiply * divide /]

# check for valid user input - operation
until valid_ops.include?(op)
puts "Invalid input."
puts "Please choose one operator. You can enter the name or the symbol."
op = gets.chomp
end

# method to verify user input - integer
def integer?
Integer(gets) rescue false
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've named this method integer? with a question mark, which implies that it should return a Boolean (true or false). However it looks like it returns the user's input as an integer. A better name for this method might be something like parse_input.


# method to print reprompt message
def number_reprompt
puts "Please enter a valid number."
end

# user input - first number
puts "First number:"
first = integer?

# check for valid user input
until first
number_reprompt
first = integer?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

end

# user input - second number
puts "Second number:"
second = integer?

# check for valid user input
until second
number_reprompt
second = integer?
end

# operation output
case op

when "add", "+"
puts "#{first} + #{second} = #{first + second}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code isn't repeated, but I think it would still increase readability to wrap this case/when 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(op, first, second).


when "subtract", "-"
puts "#{first} - #{second} = #{first - second}"

when "multiply", "*"
puts "#{first} * #{second} = #{first * second}"

when "divide", "/"

# new input prompt if try to divide by 0
while second == 0
puts "You cannot divide by zero."
puts "Please enter another number."
second = integer?

# check for valid user input
until second
number_reprompt
second = integer?
end

end

puts "#{first} / #{second} = #{first / second}"

end