-
Notifications
You must be signed in to change notification settings - Fork 49
Space - Jessica #39
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
base: master
Are you sure you want to change the base?
Space - Jessica #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| puts "Welcome to the Ruby Calculator!" | ||
|
|
||
| operator = "" | ||
| # valid operators | ||
| operators = [ | ||
| "add", "+", "1", | ||
| "subtract", "-", "2", | ||
| "multiply", "*", "3", | ||
| "divide", "/", "4", | ||
| "exponent", "**", "5", | ||
| "modulo", "%", "6" | ||
| ] | ||
|
|
||
| # prompt user for operator until they enter a valid option | ||
| until operators.include?(operator) | ||
| puts "Please choose an operation from the list (name, symbol, or number): | ||
| 1. add(+) | ||
| 2. subtract(-) | ||
| 3. multiply(*) | ||
| 4. divide(/) | ||
| 5. exponent(**) | ||
| 6. modulo(%)" | ||
| operator = gets.chomp.to_s.downcase | ||
| end | ||
|
|
||
| # update operator to operation to use in user prompt | ||
| case operator | ||
| when "add", "+", "1" | ||
| operator = "addition" | ||
| when "subtract", "-", "2" | ||
| operator = "subtraction" | ||
| when "multiply", "*", "3" | ||
| operator = "multiplication" | ||
| when "divide", "/", "4" | ||
| operator = "division" | ||
| when "exponent", "**", "5" | ||
| operator = "exponent" | ||
| when "modulo", "%", "6" | ||
| operator = "modulo" | ||
| end | ||
|
|
||
| # validate that user input is numeric | ||
| def validate_number(num) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest moving your methods to the top of the file for now, later another file, and putting the main driver code below. |
||
| while num.to_i.to_s != num.strip | ||
| puts "#{num} is not a valid number! Enter a new number: " | ||
| num = gets.chomp | ||
| end | ||
| return num | ||
| end | ||
|
|
||
| # prompt user for number inputs | ||
| puts "You chose #{operator}! Enter your first number: " | ||
| num1 = gets.chomp | ||
| num1 = validate_number(num1) | ||
|
|
||
| puts "Enter your second number: " | ||
| num2 = gets.chomp | ||
| num2 = validate_number(num2) | ||
|
|
||
| # display result based on operator | ||
| case operator | ||
| when "addition" | ||
| puts "#{num1} + #{num2} = #{num1.to_i + num2.to_i}" | ||
| when "subtraction" | ||
| puts "#{num1} - #{num2} = #{num1.to_i - num2.to_i}" | ||
| when "multiplication" | ||
| puts "#{num1} * #{num2} = #{num1.to_i * num2.to_i}" | ||
| when "division" | ||
| # prevent user from entering 0 for num2 during division | ||
| if num2 == "0" | ||
| puts "You cannot divide by 0!" | ||
| else | ||
| puts "#{num1} / #{num2} = #{num1.to_i / num2.to_i}" | ||
| end | ||
| when "exponent" | ||
| formula = "" | ||
| num2.to_i.times do |i| | ||
| formula += "#{num1} * " | ||
| end | ||
| puts "#{formula.chomp(" * ")} = #{num1.to_i ** num2.to_i}" | ||
| when "modulo" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also check for divide by 0 when using modulo. |
||
| puts "#{num1} % #{num2} = #{num1.to_i % num2.to_i}" | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
whenshould be indented under thecase.