For the challenge, you can use an estimate of 4 weeks per month and 12 months per year.
Activity: Assignment Operators
Assignment Operators are assignments that also perform an operator on the variable. They are used in JavaScript to modify the value.
Concept: change by
The change by block was very commonly used to modify the value a variable was assigned to when programming in Blocks.
This can be accomplished in JavaScript by using the variable itself in the equation:
This works because the equation on the right side of the assignment operator is evaluated first, and the result of that equation is assigned to the variable on the right side.
For example, if num stored 4
before the code above,
the right hand side of the equation would first be evaluated to 9
,
and then num would be reassigned 9
as it’s new value.
Modifying a value in this way is a very common task, so JavaScript
and other languages introduce the +=
operator,
which will add the value on the right side to the value
currently stored on the left side.
Using the +=
operator, the example code above can also be written as
This process of condensing the expression extends to all operators, so…
num -= 5;
is equivalent tonum = num - 5;
num *= 5;
is equivalent tonum = num * 5;
num /= 5;
is equivalent tonum = num / 5;
These are called Assignment Operators, like =
.
Example #1: Seconds in an Hour
- Review the code below
- Observe how assignment operators are used to modify the seconds variable
- Identify the two values that are splashed
Student Task #1: Seconds in a Week
- Start with the code from example #1
- After the second game.splash, use an assignment operator to make seconds store the number of seconds in a day
- Splash the new value of seconds. Make sure to include a description of the value to match the previous values that were splashed
- Use another assignment operator to make seconds store the number of seconds in a week
- Splash the new value of seconds, along with it’s corresponding description
- Challenge: repeat this process for both the seconds in a month, and seconds in a year
Concept: Increment and Decrement
In JavaScript, it is very common to add or subtract one from a value: for example, to count the number of times a button was pressed, or the index in a loop.
In JavaScript, there are several Assignment Operators that can do this.
For example, both num = num + 1;
and num += 1;
will result in
num being incremented by 1.
However, because this is such a common task, there is an even shorter way to write it.
This is done using the Increment Operator, represented by two plus signs.
num++;
is equivalent to the two expressions above.
Similarly, the Decrement Operator is represented by two minus signs,
meaning num = num - 1;
is the same as both num -= 1;
and num--;
.
Example #2: Incrementing Values
- Review the code below
- Identify how the increment operator is used to change the value of myNumber
Student Task #2: Just Right
- Start with the code from example #2
- Use the decrement operator to lower of myNumber by 1
- Splash myNumber with the description “ is just right!”
Concept: Appending Strings
The Assignment Operator += can also be useful when adding to strings.
Values can be appended using the += operator. For example, the following snippet:
can also be written:
This allows for strings to be “built” by adding pieces to them as necessary.
Example #3: Food Order
- Review the code written below
- Identify how the += operator is used to build up an order string
Student Task #3: Dessert
- Copy the code from the example above
- Modify the code so that it also prompts the user “What dessert would you like”
- Append to order the sentence “ For dessert, I would like “ followed by the dessert the user requested
- Append a “.” to end the sentence
- Challenge: instead of storing the variables food, drink, and dessert, append the result of game.askForString directly to order
What did we learn?
- Explain how the different increment, decrement, and assignment operators can be useful for modifying a number variable.