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 trialSteve Burgos
2,759 PointsHow do I make initialScore and totalScore equal to each other
How do I make the totalScore equal to initialScore. I am having trouble understanding what the question is asking of me.
2 Answers
Steven Deutsch
21,046 PointsHey Steve Burgos,
The question is asking you to increase the value of initialScore by 1 and then after you do that, you need to assign the resulting value to a constant named totalScore. We can do this using the increment operator (++).
var initialScore = 8
let totalScore = ++initialScore
/* what this code is doing, is taking the value of initial score and increasing it by 1
THEN the resulting value stored in the totalScore constant
AND initialScore will have the same value as totalScore
remember the = sign is the "assignment operator",
what we do on the right side of the = we assign to the left side */
Note that that the placement of the increment operator matters!
var initialScore = 8
let totalScore = initialScore++
/* this would not be correct because it would first set the value of totalScore to initialScore
THEN it would increment initialScore AFTER that.
So, totalScore would be the old value of 8 before it was incremented
and the new value of initialScore would be 9.
*/
Good Luck!
Jhoan Arango
14,575 PointsHello:
The challenge may be confusing at first, but if you watch the previews video again, then the challenge may make a bit more sense.
So Pasan explains that to add a single value to a numeric variable there is an operator called "increment operator". With this operator you can increment or decrement a numeric value from a variable.
// increment (++)
// decrement (--)
var number = 1
++number // This will increment the variable "number" to 2
--number // this will decrement the variable "number" to 1
I am going to copy and paste an explanation from "The Swift Programming language" book, which I believe will help you understand this better.
Increment and Decrement Operators
Like C, Swift provides an increment operator (++) and a decrement operator (--) as a shortcut to increase or decrease the value of a numeric variable by 1. You can use these operators with variables of any integer or floating-point type.
var i = 0
++i // i now equals 1
Each time you call ++i, the value of i is increased by 1. Essentially, ++i is shorthand for saying i = i + 1. Likewise, --i can be used as shorthand for i = i - 1.
The ++ and -- symbols can be used as prefix operators or as postfix operators. ++i and i++ are both valid ways to increase the value of i by 1. Similarly, --i and i-- are both valid ways to decrease the value of i by 1.
Note that these operators modify i and also return a value. If you only want to increment or decrement the value stored in i, you can ignore the returned value. However, if you do use the returned value, it will be different based on whether you used the prefix or postfix version of the operator, according to the following rules:
- If the operator is written before the variable, it increments the variable before returning its value.
- If the operator is written after the variable, it increments the variable after returning its value. For example:
var a = 0
let b = ++a
// a and b are now both equal to 1
let c = a++
// a is now equal to 2, but c has been set to the pre-increment value of 1
In the example above, let b = ++a increments a before returning its value. This is why both a and b are equal to the new value of 1.
However, let c = a++ increments a after returning its value. This means that c gets the old value of 1, and a is then updated to equal 2.
Unless you need the specific behavior of i++, it is recommended that you use ++i and --i in all cases, because they have the typical expected behavior of modifying i and returning the result.