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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions 4-loops/Review.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,33 @@ 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
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!

// 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 the lowest prime number
if checkPrime == 2 {
isPrime = true
}
// All numbers under 2 are not prime
if checkPrime < 2 {
isPrime = false
}

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


// Third Challenge
Expand All @@ -45,4 +58,4 @@ var stopNum = 4
for _ in 1...stopNum {
starTracker += "*"
print(starTracker)
}
}