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
55 changes: 55 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#Calculator Assignment

#created an array of all the possible operations
operations_array = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]

#display a list of operations to the user
puts "Please select from below types of operations: "
puts "1. add (+)"
puts "2. subtract (-)"
puts "3. multiply (*)"
puts "4. divide (/)"

#give user an option to choose the type of operation
operation = gets.chomp

#check if input is valid by searching in the operations_array
until operations_array.include?(operation)
puts "Please enter a valid input"
operation = gets.chomp
end

#a method to check if the input from the user is valid
def check_number
input = gets.chomp
until input =~ /\d/
puts "Enter a valid number"
input = gets.chomp
end
return input.to_f
end

#call the method on user input to verify for valid input
puts "Enter your first number"
num1 = check_number

puts "Enter your second number"
num2 = check_number

#case when syntax that checks which operation to perform based on the value of the variable operation
case operation
when "add", "+"
puts "#{num1 + num2}"
when "subtract", "-"
puts "#{num1 - num2}"
when "multiply", "*"
puts "#{num1 * num2}"
when "divide", "/"
# puts "#{num1 / num2}"
if num2 == 0
puts "infinity"
else
puts "#{num1 / num2}"
end
end