Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Lana Wong
3,968 PointsI do not understand
When I tried this, the point instance is the same. Could you tell me why, and what is wrong with my code? Thank you.
let x1 = 0 let y1 = 0
let coordinate1: (x: Int, y: Int) = (0,0) coordinate1.x
struct Point{ let x: Int let y: Int
init(x: Int, y: Int){
self.x = x
self.y = y
}
func surroundingPoints(withRange range: Int = 1) -> [Point]{ var results: [Point] = [] for xCoord in (x-range)...(x+range) { for yCoord in (y-range)...(y+range) { let corrdinatePoint = Point(x: xCoord, y: yCoord) results.append(coordinatePoint) } } return results
} }
let coordinatePoint = Point(x: 0, y: 0) coordinatePoint.surroundingPoints()
1 Answer
Jeroen de Vrind
29,772 PointsHi Lana, a few things you can get rid off first: you don't use x1, y1 and coordinate1 so u can remove this. Just like the initializer. Structs give u this initializer for free, so you don't need to write them yourself. With classes it's another case, then you should add the init method. You can remove the init method for this struct.
Because you added a default argument to the parameter in your surroundingPoints(with Range:) method, you get the same results based on this default argument every time when you call this` function without values for this argument, like you did in:
coordinatePoint.surroundingPoints()
If you call this method with a value for the argument, e.g.
coordinatePoint.surroundingPoints(withRange:3)
I'm sure you'll get a different result.
Lana Wong
3,968 PointsLana Wong
3,968 PointsWhen posting, the code became all on one line, so it looks very weird.