-
Notifications
You must be signed in to change notification settings - Fork 48
Val - Calculator - Edges #45
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?
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 @@ | ||
| # Calculator Project - 8.8.18 | ||
|
|
||
| # print welcome message and operator options | ||
| puts "Welcome to The Calculator! Which operator would you like to use?" | ||
| puts "1. add(+)" | ||
| puts "2. subtract(-)" | ||
| puts "3. multiply(*)" | ||
| puts "4. divide(/)" | ||
|
|
||
| # user input - operation | ||
| puts "Please choose one operator. You can enter the name or the symbol." | ||
| op = gets.chomp | ||
|
|
||
| valid_ops = %w[add + subtract - multiply * divide /] | ||
|
|
||
| # check for valid user input - operation | ||
| until valid_ops.include?(op) | ||
| puts "Invalid input." | ||
| puts "Please choose one operator. You can enter the name or the symbol." | ||
| op = gets.chomp | ||
| end | ||
|
|
||
| # method to verify user input - integer | ||
| def integer? | ||
| Integer(gets) rescue false | ||
| end | ||
|
|
||
| # method to print reprompt message | ||
| def number_reprompt | ||
| puts "Please enter a valid number." | ||
| end | ||
|
|
||
| # user input - first number | ||
| puts "First number:" | ||
| first = integer? | ||
|
|
||
| # check for valid user input | ||
| until first | ||
| number_reprompt | ||
| first = integer? | ||
|
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've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method? |
||
| end | ||
|
|
||
| # user input - second number | ||
| puts "Second number:" | ||
| second = integer? | ||
|
|
||
| # check for valid user input | ||
| until second | ||
| number_reprompt | ||
| second = integer? | ||
| end | ||
|
|
||
| # operation output | ||
| case op | ||
|
|
||
| when "add", "+" | ||
| puts "#{first} + #{second} = #{first + second}" | ||
|
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. This code isn't repeated, but I think it would still increase readability to wrap this |
||
|
|
||
| when "subtract", "-" | ||
| puts "#{first} - #{second} = #{first - second}" | ||
|
|
||
| when "multiply", "*" | ||
| puts "#{first} * #{second} = #{first * second}" | ||
|
|
||
| when "divide", "/" | ||
|
|
||
| # new input prompt if try to divide by 0 | ||
| while second == 0 | ||
| puts "You cannot divide by zero." | ||
| puts "Please enter another number." | ||
| second = integer? | ||
|
|
||
| # check for valid user input | ||
| until second | ||
| number_reprompt | ||
| second = integer? | ||
| end | ||
|
|
||
| end | ||
|
|
||
| puts "#{first} / #{second} = #{first / second}" | ||
|
|
||
| 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.
You've named this method
integer?with a question mark, which implies that it should return a Boolean (trueorfalse). However it looks like it returns the user's input as an integer. A better name for this method might be something likeparse_input.