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 Working With Operators: Part 2

Wrong answer for task one?

For the first task it asks:"For this task to pass, we want both totalScore and initialScore to have the same value after the increment operation." But when I did "var initialScore = 8; let finalScore = initialScore++" it showed "wrong answer". And it showed "correct" when I change the code to "let finalScore = ++initialScore". As far as I know, the resultant value for the second code would be "initialScore = 8, finalScore = 9". Doesn't it contradict with the requirement of the task?

1 Answer

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

There is a subtle difference between "let finalScore = initialScore++" and "let finalScore = ++initialScore".

"let finalScore = initialScore++" is equivalent to the following code:

let finalScore = initialScore // finalScore has been set to 8
initialScore += 1 // initialScore is now 9

"let finalScore = ++initialScore" is equivalent to the following code:

initialScore += 1 // initialScore is now 9
let finalScore = initialScore // finalScore has been set to 9

I hope this helps!