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
63 changes: 63 additions & 0 deletions faezeh_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# When a user runs this program, the program should ask the user for the following things:

# 1- a math operation
# 2- a number for the math operation
# 3- a second number for the math operation

puts "I am glad you chose my calculator, because it's awesome!
Which operator woudl you like to use?
1. add(+)
2. subtract(-)
3. multiply(*)
4. divide (/)
Please choose one operator (name or symbol)"

command = gets.chomp.downcase

# operators = ["add", "+", "subtract", "-",
# "multiply", "*", "devide", "/"]

# operators.each do |i|
# puts i + "(" #{i+1} + ")"
# end

until ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", ].include?(command)
puts "What you entered is not an option!
Please tell me to add (+), subtract (-) multiply (*) or devide (/)!"
command = gets.chomp.downcase
end

class Object
def is_number?
to_f.to_s == to_s || to_i.to_s == to_s
end
end
Comment on lines +30 to +34

Choose a reason for hiding this comment

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

Clever. Did you find this on Stack Overflow or someplace. It's best to leave a comment with a link to where you find something like this.

Copy link
Author

Choose a reason for hiding this comment

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

I had found it from stack overflow when I was writing code for the technical challenge. I will comment the source from now on when I find something. thank you for reminding me.


num_1_str = "first"
until num_1_str.is_number?
puts "Please provide your first number:"
num_1_str = gets.chomp
end
num_1 = num_1_str.to_f

num_2_str = "second"
until num_2_str.is_number?
puts "Please provide your second number:"
num_2_str = gets.chomp
end
num_2 = num_2_str.to_f

case command
when "add", "+"

Choose a reason for hiding this comment

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

Really the when lines should be indented under the case.

Copy link
Author

Choose a reason for hiding this comment

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

Really the when lines should be indented under the case.

Chris, it seems that I have installed an add on on VS code that is supposed to fix all the indentations when I save the file, but in this case it un-indents the "when" blocks. Is there a way I can fix this?

puts "We're adding numbers"
puts "#{num_1} + #{num_2}: #{num_1 + num_2}"
when "subtract", "-"
puts "We're subtracting numbers"
puts "#{num_1} - #{num_2}: #{num_1 - num_2}"
when "multiply", "*"
puts "We're multiplying numbers"
puts "#{num_1} * #{num_2}: #{num_1 * num_2}"
when "divide", "/"
puts "We're dividing numbers"
puts "#{num_1} / #{num_2}: #{num_1 / num_2}"
end