The aim of this puzzle: Use the subtraction assignment -=
operator to subtract from a variable.
Walkthrough of the solution: When updating the value of a variable, a new value must be assigned to the variable. The usual way to do this would be something like:
let x = 5;
x = x - 2;
This tells the code, “Take the current value of x
(which is 5), subtract 2, and reassign it to x
.” x
would now equal 3.
A shorter way to write this is to use the -=
operator:
let x = 5;
x -= 2;
Is the same as:
let x = 5;
x = x - 2;
To complete this puzzle, use the -=
operator to subtract purchase
from wallet
.
Sample code solution:
(Tap below to reveal)
let wallet = 80;
let purchase = 35;
wallet -= purchase;
console.log('Wallet is now ' + wallet);
JavaScript Concepts: Subtraction Assignment Operator, Variable Declarations with Let