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
58 changes: 58 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# check user input to chcek if it contains only numbers
def check_number (number)
until number.match(/\A\d+\z/)
puts "Invalid Operation.Please enter the number again"
number = gets.chomp
end

number.to_i
end

# perform math operations based on user input
# display results to the user

def calculator
valid_operations = ["add","+","subtract","-","multiply","*","divide","/"]

# ask user for math operations
# check if operation is valid and if not ask for a new input
puts "What is the math operation?"
operation = gets.chomp.strip.downcase
until valid_operations.include?(operation)
puts "Please enter a valid operation"
operation = gets.chomp
end


# ask user for numbers and input validation
puts "What is the first number for the math operation?"
number_one = gets.chomp.strip
number_one = check_number(number_one)
puts "What is the seccond number for the math operation?"
number_two = gets.chomp.strip
number_two = check_number(number_two)

# use user input to do math operations
if operation == "add" || operation == "+"
result = number_one + number_two
operation = "+"
elsif operation == "subtract" || operation == "subtract"
result = number_one - number_two
operation = "-"
elsif operation == "multiply" || operation == "*"
result = number_one * number_two
operation = "*"
elsif operation == "divide" || operation == "/"
result = number_one / number_two
operation = "/"

end


# display result of calculation to user
puts " #{number_one} #{operation} #{number_two} = #{result}"
end



calculator