-
Notifications
You must be signed in to change notification settings - Fork 105
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
update to swift.review #31
Open
jdwagner17
wants to merge
3
commits into
Codecademy:main
Choose a base branch
from
jdwagner17:update-swift.review
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hey @jdwagner17, curious why this was changed, from true to false.
If we keep it as true, then we could avoid adding the else statement and block in line 25.
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.
I think it may be "best practice" to do it the way you suggested.
I included this because it may help new programmers better understand the relationship. If the variable is something we don't control (for whatever reason), they will know how to compensate for an unknown. That may not exist in Swift, as I noticed I could not do
var isPrime = Bool
The situation I am imagining may not exist.
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.
Ahhh, gotcha. So you'd want
var isPrime: Bool
. Which would type the variable, but not assign a value yet. This would work!However, right now for line 25, with the
else
statement, you're reassignisPrime
during each iteration of the loop. You don't need to do this, rather, you can make that reassignment happen outside the scope of the loop (currently on line 30 where the comment is)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.
var isPrime: Bool makes sense to me, but I get an error when used in CodeAcademy.
Review.swift:64:43: error: variable 'isPrime' used before being initialized
print("Is (checkPrime) a prime number? (isPrime)!")
^
Review.swift:44:5: note: variable defined here
var isPrime: Bool
^
I think I understand your comment about iterations within the loop. Will fix in commit
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.
Yep, I think this is something happening under the hood, where Swift's interpreter sees a situation where
isPrime
might not get assigned at all before the print statement.Try fixing the assignment issue of
isPrime
being assigned during each iteration and that might resolve this issue as well :)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.
Hope you had a nice Easter!
There is an issue that I don't understand. I think it is why you see a simpler way to do this, and I don't. I'm going to show you new code I made and the output from CodeAcademy.
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.
Hope you also had a nice Easter :)
You can also share code with me via Codecademy's workspaces. Here's a sample that I made to show you what's "wrong" with your code. https://www.codecademy.com/workspaces/6435a2a522be61c54cbe68de
Something that's really helpful for me is using
print()
statements to check out values.So something things I noticed:
isPrime
is now initialized astrue
if
condition is thatcheckPrime > 2
then you go through the loop (and reassignment, etc..).Do you see what's wrong? You can uncomment lines 10 and 16 to help you out :) (I think you might have to fork my workspace to alter the code)
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.
Ugh! Yes it's obvious what is wrong. Once I read...
"Your if condition is that checkPrime > 2 then you go through the loop (and reassignment, etc..)."
...I knew what I had done.
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.
And these things happen!
I'd still say trying to debug on your own (with print statements) is helpful and useful later down the line too.
And also, be kind to yourself!