Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jdwagner17
Copy link

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.

…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.
@CLAassistant
Copy link

CLAassistant commented Apr 3, 2023

CLA assistant check
All committers have signed the CLA.

@jdwagner17
Copy link
Author

@LinKCoding I believe this is now ready

Copy link
Contributor

@LinKCoding LinKCoding left a 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
Copy link
Contributor

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.

Copy link
Author

@jdwagner17 jdwagner17 Apr 6, 2023

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.

Copy link
Contributor

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)

Copy link
Author

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

Copy link
Contributor

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 :)

Copy link
Author

@jdwagner17 jdwagner17 Apr 11, 2023

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.

isPrime

Copy link
Contributor

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:

  1. isPrime is now initialized as true
  2. Your if condition is that checkPrime > 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)

Copy link
Author

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.

Copy link
Contributor

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 Show resolved Hide resolved
}

print("Is \(checkPrime) a prime numer? \(isPrime)!")
print("Is \(checkPrime) a prime number? \(isPrime)!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

// 2 is a prime number
} else if checkPrime == 2 {
isPrime = true
// all numbers under 2 are not prime
Copy link
Contributor

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 :)

Copy link
Author

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").

Copy link
Contributor

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)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants