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

iOS Swift 2.0 Basics Swift Operators The End

Megan Sheehan
Megan Sheehan
1,835 Points

Questions about last code challenge, unsure of how to complete the task.

Can someone shed some light on why this was not correct for the last code challenge: Let's assume we're creating a silly game. In this game every time a user completes an objective (whatever it is) we increment their total score by 1 point.

In the editor below, initialScore is the players current score. Assuming they completed a single objective for this level, let's add 1 point to their score using the increment operator and assign the result to a constant named totalScore.

Remember that the position of the unary increment operator matters. For this task to pass, we want both totalScore and initialScore to have the same value after the increment operation.

My code:

var initialScore = 8

initialScore = initialScore + 1

var totalScore = initialScore

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Your code would have the same result as what the challenge is expecting. But the challenge explicitly asks for the increment operator ++. And as the challenge notes, the placement of this is key. Just because something is functional and has the same result outside of the challenge doesn't necessarily mean that it fulfills the challenge requirements. Hope that makes sense! :sparkles:

Megan Sheehan
Megan Sheehan
1,835 Points

Thank you so much for your response Jennifer! I was able to figure it out with your hints. Thank you thank you!

var initialScore = 8

let totalScore = initialScore

initialScore = ++initialScore

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You're sooooooo close here. And in fact, you're so close I'm just going to give another hint. Your code should only be two lines long. The beginning declaration of initialScore and the beginning declaration of totalScore which should have the properly incremented initalScore assigned to it. Try and get what you have there in two lines of code :smiley:

Megan Sheehan
Megan Sheehan
1,835 Points

Thanks Jennifer, I see that this answer is accepted as well:

var initialScore = 8

let totalScore = ++initialScore

I know that future versions of swift '++' will be depreciated and removed in swift 3. Is that correct though?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes, it will be (and possibly is now) deprecated. You will be using let y += 1 for example :smiley:

Megan Sheehan
Megan Sheehan
1,835 Points

Ok, I think I got it! If you have any recommendations for future reading let me know. I appreciate all your help!