From 41c2a11dd30fabb3a70bbca2ad64dc66b4eea8b5 Mon Sep 17 00:00:00 2001 From: katemyer <51521448+katemyer@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:27:09 -0800 Subject: [PATCH 1/2] Create calculator.rb Calculator project from day 2 --- calculator.rb | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..fcc6b97 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,69 @@ +# Calculator Program + +# Methods +def add(num_one, num_two) + return num_one + num_two +end + +def subtract(num_one, num_two) + return num_one - num_two +end + +def multiply(num_one, num_two) + return num_one * num_two +end + +def divide(num_one, num_two) + return ((num_one.to_f) / (num_two.to_f)).round(2) +end + +def ValidOperator (operator) + until ["Add","+", "Subtract","-", "Multiply", "*", "Divide", "/"].include? (operator) + puts "Enter a valid selection" + operator = gets.chomp + end + return operator +end + +def ValidateNumber (num) + until num == num + puts "Enter a valid number" + num = gets.chomp.to_i + end + return num +end + + +puts "Welcome to the Le Calcuator Program! Enter the operator would you like to use?" +puts "-----" +operator_array = ["Add(+)", "Subtract(-)", "Multiply(*)", "Divide(/)"] +puts operator_array +user_operator = gets.chomp +user_operator = ValidOperator(user_operator) + +puts "Enter your first number." +num_one = gets.chomp.to_i +num_one = ValidateNumber(num_one) #validatenumber calls the method, passes the argument num_one, and reassigns variable to the returned value of the method + +puts "Enter your second number." +num_two = gets.chomp.to_i +num_two = ValidateNumber(num_two) + +# Equating the word "add" to the operator +, etc and incorporating methods +case user_operator +when "add", "+" + puts "We're adding numbers" + answer = add(num_one, num_two) + puts "Your answer is #{answer}." +when "subtract", "-" + answer = subtract(num_one, num_two) + puts "Your answer is #{answer}." +when "multiply", "*" + answer = multiply(num_one, num_two) + puts "Your answer is #{answer}." +when "divide", "/" + answer = divide(num_one, num_two) + puts "Your answer is #{answer}." +else + puts "What do you want from me?!" +end From e6c4df3e8f4afe641db8f611bb33a49cd2c2e48b Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Thu, 6 Feb 2020 21:41:40 -0800 Subject: [PATCH 2/2] Addressed comments from Chris. --- calculator.rb | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/calculator.rb b/calculator.rb index fcc6b97..4b51806 100644 --- a/calculator.rb +++ b/calculator.rb @@ -17,15 +17,15 @@ def divide(num_one, num_two) return ((num_one.to_f) / (num_two.to_f)).round(2) end -def ValidOperator (operator) +def valid_operator (operator) until ["Add","+", "Subtract","-", "Multiply", "*", "Divide", "/"].include? (operator) - puts "Enter a valid selection" - operator = gets.chomp + puts "Enter a valid selection" + operator = gets.chomp.downcase end return operator end -def ValidateNumber (num) +def validate_number (num) until num == num puts "Enter a valid number" num = gets.chomp.to_i @@ -33,37 +33,36 @@ def ValidateNumber (num) return num end - puts "Welcome to the Le Calcuator Program! Enter the operator would you like to use?" puts "-----" operator_array = ["Add(+)", "Subtract(-)", "Multiply(*)", "Divide(/)"] puts operator_array -user_operator = gets.chomp -user_operator = ValidOperator(user_operator) +user_operator = gets.chomp.downcase +user_operator = valid_operator(user_operator) puts "Enter your first number." num_one = gets.chomp.to_i -num_one = ValidateNumber(num_one) #validatenumber calls the method, passes the argument num_one, and reassigns variable to the returned value of the method +num_one = validate_number(num_one) #validatenumber calls the method, passes the argument num_one, and reassigns variable to the returned value of the method puts "Enter your second number." num_two = gets.chomp.to_i -num_two = ValidateNumber(num_two) +num_two = validate_number(num_two) # Equating the word "add" to the operator +, etc and incorporating methods case user_operator -when "add", "+" - puts "We're adding numbers" - answer = add(num_one, num_two) - puts "Your answer is #{answer}." -when "subtract", "-" - answer = subtract(num_one, num_two) - puts "Your answer is #{answer}." -when "multiply", "*" - answer = multiply(num_one, num_two) - puts "Your answer is #{answer}." -when "divide", "/" - answer = divide(num_one, num_two) - puts "Your answer is #{answer}." -else - puts "What do you want from me?!" -end + when "add", "+" + puts "We're adding numbers" + answer = add(num_one, num_two) + puts "Your answer is #{answer}." + when "subtract", "-" + answer = subtract(num_one, num_two) + puts "Your answer is #{answer}." + when "multiply", "*" + answer = multiply(num_one, num_two) + puts "Your answer is #{answer}." + when "divide", "/" + answer = divide(num_one, num_two) + puts "Your answer is #{answer}." + else + puts "What do you want from me?!" +end \ No newline at end of file