Arithmetic Shortcuts
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 14 of 77.
JavaScript created a cool shortcut for self-arithmetic operations.
For example, instead of writing:
let a = 5
a = a + 3 // a holds 8We can simplify it by writing +=:
let a = 5
a += 3 // a holds 8The += is adding to a itself the value 3
This operation is valid for all arithmetic operations:
| Operator | Shortcut |
|---|---|
| + | += |
| - | -= |
| * | *= |
| / | /= |
| % | %= |
Challenge
BeginnerYou are given a code with initialization of count. (Don't delete this line!)
Your task is to add the following operations, in this order:
- Add
4tocount - Multiply
countby2 - Subtract
1fromcount
Use the arithmetic shortcuts to do so!
Cheat sheet
JavaScript provides shortcut operators for self-arithmetic operations:
| Operator | Shortcut |
|---|---|
| + | += |
| - | -= |
| * | *= |
| / | /= |
| % | %= |
Instead of writing:
let a = 5
a = a + 3 // a holds 8You can use the shortcut:
let a = 5
a += 3 // a holds 8Try it yourself
// Type your code below
let count = 0
// Don't change the line below
console.log(`count = ${count}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False