Arrays and Sets - Mixtape.swift Project

Hey All,

I finished the Mixtape.swift project with relative ease. I am having issues with the “optional” challenge though.

"Create a tracklist for the two sides of the compilation tape, “Side A” and “Side B”, using `for-in` loop(s). For example:

"

I can’t for the life of me figure this out. The Github solution has elements in the loop to make this possible that we have not gone over yet and I can’t make head over tails of what it means. Please see below:

print(“Side A\n”)

for i in 0 …< mixtape.count/2 {
print("(i+1). (mixtape[i])")
}

print()

// ======
// Side B
// ======
print(“Side B\n”)

for i in mixtape.count/2 …< mixtape.count {
print("(i+1). (mixtape[i])")
}

I suppose what I really just want to know is how this aspect of the code works/what is it “saying,”:

for i in 0 …< mixtape.count/2 {
print("(i+1). (mixtape[i])")

I want to understand how this works as being able to create a numbered ordered list for an array in a loop would be very good know for future projects.

Thanks!

Hi,

So you’re wondering how the for-in loop is working? Take a look at swift documentation as it’s a great resource: Control Flow — The Swift Programming Language (Swift 5.5)

In a nutshell, `0...x` is a range of number for 0 through x which is inclusive of x. Swift provides the additional flexibility of specifying a condition `0...<x` which is 0 through all integers less than x … so exclusive of x.

Interesting, I understand a range in a loop can look like this 0…10, but I didn’t know one could make the beginning or end of a range with a variable count, let alone adding an operator sign on top of it. So, if I understand correctly, when the code is “saying” is…

``````print(“Side A\n”)

//for "i." (i indicating the track number) in a loop count starting from more than 0 to "mixtape.count (10) / 2", so 1-5.
for i in 0 …< mixtape.count/2 {
//(0+1) looped 5 times, then "(mixtape[i])" "i" being the track number from each new loop up to track 5.
print("(i+1). (mixtape[i])")
}

print()

// ======
// Side B
// ======
print(“Side B\n”)

// i has a range from anything more than "mixtape.count (10) / 2, so 5) to "mixtape.count (10), so 6-10"
for i in mixtape.count/2 …< mixtape.count {
//(5+1) looped 5 times, then "(mixtape[i])" "i" being the track number from each new loop up to track 10.
print("(i+1). (mixtape[i])")
}
``````

That’s completely right.

Thank you for explaining this to me, I appreciate it, it makes more sense to me now. I enjoy getting the code to ‘work’ but I want to make sure I understand why, so thank you for that.