-
Notifications
You must be signed in to change notification settings - Fork 48
Naheed - Calculator - Edges #32
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,26 @@ | ||
| puts "Welcome to your calculator. Choose from the following operations: \n 1. add (+) \n 2. subtract (-) \n 3. multiply (*) \n 4. divide (/) \n Please type the name or symbol for the operator you would like to use" | ||
|
|
||
| chosen_operator = gets.chomp! | ||
|
|
||
| #gets numbers from user | ||
| puts "First number:" | ||
| first_number = gets.chomp.to_f | ||
| puts "Second number:" | ||
|
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. Watch your indentation. I don't think these lines should be indented at all. |
||
| second_number = gets.chomp.to_f | ||
|
|
||
| #checking if number is integer by dividing it by itself | ||
| if first_number / first_number == 1 && second_number / second_number == 1 | ||
|
|
||
|
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 is a creative way to address this problem. Unfortunately it's got one big issue: it doesn't work for 0! |
||
| if chosen_operator == "add" || chosen_operator == "+" | ||
| puts "#{first_number} + #{second_number} = #{first_number + second_number}" | ||
| elsif chosen_operator == "subtract" || chosen_operator == "-" | ||
| puts "#{first_number} - #{second_number} = #{first_number - second_number}" | ||
|
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 |
||
| elsif chosen_operator == "multiply" || chosen_operator == "*" | ||
| puts "#{first_number} * #{second_number} = #{first_number * second_number}" | ||
| elsif chosen_operator == "divide" || chosen_operator == "/" | ||
| puts "#{first_number} / #{second_number} = #{first_number / second_number}" | ||
| end | ||
|
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 have an |
||
|
|
||
| #if number wasn't an integer, will display option to re-input | ||
| elsif puts "Sorry, that wasn't a number. Try again." | ||
| 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 written almost exactly the same code twice to get the two numbers from the user. As is it's only two lines, so it's not so bad, but it makes it difficult to expand that logic. For example, you might want to run this code in a loop until the user enters a valid number. Doing this in a method would mean you only have to write that code once.