-
Notifications
You must be signed in to change notification settings - Fork 108
update to swift.review #31
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: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -11,20 +11,31 @@ for num in 1...100 { | |
} | ||
|
||
|
||
// Second Challenge | ||
// checkPrime can be changed to a positive number greater than 1 | ||
var checkPrime = 17 | ||
// Assume the number is prime until proven otherwise | ||
var isPrime = true | ||
for num in 2...checkPrime - 1 { | ||
// If checkPrime is fully divisible by the current number it's not a prime number | ||
if checkPrime % num == 0 { | ||
isPrime = false | ||
break | ||
// Second Challenge | ||
// checkPrime can be any integer | ||
var checkPrime = 5 | ||
// isPrime may be initialized with either a true or false value | ||
var isPrime = false | ||
// checkPrime will be divisble by no number other than itself and 1 if it is a prime number | ||
if checkPrime > 2 { | ||
for num in 2...checkPrime - 1 { | ||
LinKCoding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if checkPrime % num == 0 { | ||
isPrime = false | ||
break | ||
} else { | ||
// isPrime = true when a number larger than 2 could not be divided by num | ||
isPrime = true | ||
} | ||
} | ||
// 2 is a prime number | ||
} else if checkPrime == 2 { | ||
isPrime = true | ||
// all numbers under 2 are not prime | ||
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. Small nit, but please capitalize "all" => "All" -- it's a styling convention thing. Thanks :) 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. so start with capital letters, unless it is the name of something (variable "isPrime"). 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. Yep, if it's a sentence, then we'd start with a capitalized letter for the first word. |
||
} else { | ||
isPrime = false | ||
} | ||
|
||
print("Is \(checkPrime) a prime numer? \(isPrime)!") | ||
print("Is \(checkPrime) a prime number? \(isPrime)!") | ||
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. Good catch! |
||
|
||
|
||
// Third Challenge | ||
|
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!