-
Notifications
You must be signed in to change notification settings - Fork 103
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
base: main
Are you sure you want to change the base?
Conversation
…ar checkPrime. This also allows var isPrime to be set to either true or false from initialization. In the original code, there is a comment on line 15 that contains a small mistake The code has an error when checkPrime = 2, because a range cannot have the upper and lower bound be the same value. This can occur because the range will be 2...(2-1). maybe this works in swift, but it didn't work on Codeacademy for some reason.
@LinKCoding I believe this is now ready |
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.
Thanks @jdwagner17 I left some messages, the biggest thing I'd want some feedback on is line 18 (and how it affects lines 25-28.
Otherwise, lgtm!
// checkPrime can be any integer | ||
var checkPrime = 5 | ||
// isPrime may be initialized with either a true or false value | ||
var isPrime = false |
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 reassign isPrime
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.
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
- Your
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!
4-loops/Review.swift
Outdated
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
4-loops/Review.swift
Outdated
// 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 comment
The 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 comment
The 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 comment
The 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.
But if it's a variable (like isPrime
) or something that's always lower cased like iPad
we'd opt to keep the proper spelling.
Made an update based off of this feedback. "However, right now for line 25, with the else statement, you're reassign isPrime 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)"
update to challenge 2
allows for any integer to be determined as prime or not prime
This also allows var isPrime to be set to either true or false from initialization.