From ce1837ea4d62b90593e5f0d611f560ec4de957e9 Mon Sep 17 00:00:00 2001 From: Stephanie <59209423+stephaniejmars@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:28:31 -0800 Subject: [PATCH] Create calculator.rb --- calculator.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..6fe7d3b --- /dev/null +++ b/calculator.rb @@ -0,0 +1,68 @@ +def operator_check(operator) + case operator + when "1", "1.", "add", "+" + return "addition" + when "2", "2.", "subtract", "-", "delete" + return "subtraction" + when "3", "3.", "multiply", "*", "x", "times" + return "multiplication" + when "4", "4.", "divide", "/", "%" + return "division" + else + puts "Please enter a valid selection" + operator = gets.chomp.downcase.to_s + operator_check(operator) + end +end + +puts "Let's do some MATH!" + +puts "Please pick an operator: +1. Add (+) +2. Subtract(-) +3. Multiply (*) +4. Divide (/)" + +operator = gets.chomp.downcase.to_s +operator_n = operator_check(operator) + + +## getting input 1 +puts "Please pick the first number" +a = gets.chomp + + ## checking for valid imput +if a.to_i.to_s != a.strip + puts "Please enter a valid number" +else + a = a.to_i +end + +## getting input 2 +puts "Please pick the second number" +b = gets.chomp + +if b.to_i.to_s != b.strip + puts "Please enter a valid number" +else + b = b.to_i +end + +##calculator +def calcluator(operator, a, b) + if operator == "addition" + puts "Your answer is #{a + b}" + elsif operator == "subtraction" + puts "Your answer is #{a - b}" + elsif operator == "multiplication" + puts "Your answer is #{a * b}" + elsif operator == "division" && b != 0 + puts "Your answer is #{a / b}" + elsif operator == "division" && b == 0 + puts "Your answer is 0" + end +end + +calcluator(operator_n, a, b) + +