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
81 changes: 81 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
puts "Hello user! Which operator would you like to use?
1. add or +
2. subtract or -
3. multiply or *
4. divide or /
5. exponents or **
6. modulo or %
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)

puts "That's not a valid operator! Try again."
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_one = gets.chomp.to_s
# case num_one
# when /\D/, ""
# puts "Invalid input. Please put an integer or a float:"
# num_one = gets.chomp
# end

puts "Enter the second number: "
num_two = gets.chomp.to_s
case num_two
when /\D/, ""
puts "Invalid input. Please put an integer or a float:"
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_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 "Invalid. You can't divide by 0!"
until num_two != 0
puts "Put another second number:"
num_two = gets.chomp.to_i
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.

answer = num_one + num_two
puts "Great! #{num_one} + #{num_two} = #{answer}"
end
def subtraction(num_one, num_two)
answer = num_one - num_two
puts "Great! #{num_one} - #{num_two} = #{answer}"
end
def multiplication(num_one, num_two)
answer = num_one * num_two
puts "Great! #{num_one} * #{num_two} = #{answer}"
end
def division(num_one, num_two)
answer = num_one / num_two
puts "Great! #{num_one} / #{num_two} = #{answer}"
end
def exponent(num_one, num_two)
answer = num_one ** num_two
puts "Great! #{num_one} ** #{num_two} = #{answer}"
end
def modulos(num_one, num_two)
answer = num_one % num_two
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.

when "add", "+"
addition(num_one, num_two)
when "subtract", "-"
subtraction(num_one, num_two)
when "multiply", "*"
multiplication(num_one, num_two)
when "divide", "/"
division(num_one, num_two)
when "exponents", "**"
exponent(num_one, num_two)
when "modulo", "%"
modulos(num_one, num_two)
end