Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipes - <Salome Wubeshet> - <Calculator> #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Baseline

# The program should ask the user for an operation (string or numeric symbol) and two numbers.

# Primary Requirements

# The program should use the input operation and two numbers to provide the result of applying the
# operation to the two numbers.

# The program should have support for these four operations: addition, subtraction, multiplication,
# and division.
# The program should accept both the name (add) and the symbol (+) for each possible operation.

puts "give me a an operation, it can be a string or numeric symbol:"
operation = gets.chomp

puts "Please give me 2 numbers you'd like to do math on:"
num1 = gets.chomp.to_i
num2 = gets.chomp.to_i


def add(num1, num2)
return num1 + num2
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using tabs for indentation - please use two spaces per level of indentation instead. It doesn't make a big difference one way or the other, but keeping our style consistent across the class will make it much easier when we start doing group projects.

You can set your editor up to do this automatically, let me know if you need help with this.


def subtract(num1, num2)
return num1 - num2
end

def multiply(num1, num2)
return num1 * num2
end

def division(num1, num2)
return num1 / num2
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you've split these out into separate methods! It may seem a little pedantic for things like + and -, but for more complicated methods (imagine finding the least common multiple of two numbers, or even checking for zero before doing division) it results in much cleaner code.


if (operation =='add' || operation == '+'|| operation == 'addition' )
result = add(num1, num2)
puts "#{result}"
elsif(operation =='subtract' || operation == '-'|| operation == 'minus' )
result = subtract(num1, num2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got many lines here that all say puts #{result}. Would it be possible to DRY this code up somehow?

puts "#{result}"
elsif (operation =='multiply' || operation == '*'|| operation == 'product' )
result = multiply(num1, num2)
puts "#{result}"
elsif (operation =='division' || operation == '/'|| operation == 'divide' )
result = division(num1, num2)
puts "#{result}"
else
puts "Please check if you have mispelled the operation type and try again..."
end