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 trialVictor Kitograd
236 PointsI need help on this code challange
I can't figure out how to do this code challenge. Can someone help me out?
1 Answer
Nathan Tallack
22,160 PointsYeah, this one is a little tricky.
In the first task they are teaching you about the increment operator (++) and how it is used. If you place it before an incrementable value (like an integer) it will increment the value before it reads it, whereas if you placed it after the incremental value it will increment it after it has read it.
Consider the following code examples. This challenge task wants you to use the first example.
var initialScore = 8
let totalScore = ++initialScore
Here we are incrementing the value of initialScore to 9 before we assign it to totalScore. Then they will both equal 9.
var initialScore = 8
let totalScore = initialScore++
Here we are incrementing the value of initialScore to 9 after we assign it to totalScore. Then totalScore will be 8 (because the increment has not happened when we read it) and totalScore will be 9 (because it is incremented after we read it to set totalScore).
The second task wants you to set the value of isWinner to true unless the value of totalScore is 10.
var initialScore = 8
let totalScore = ++initialScore
let isWinner = totalScore != 10
So in this case as long s the value is not 10 isWinner will be true. :)