icon picker
4. Add One to a Number with the Increment Operator (++)

Increment operator

Here's a tip. It's so common in JavaScript to increment a number value by one that there's a shorthand operator (called the "increment operator") that's used frequently with loops. Use it on your counter variable by replacing += 1 with two plus symbols (++).
Open the filedo-while.js.
In thedoblock, use the increment operator to increase the number assigned tocounterby one each time the loop runs:

let counter = 0; // Creamos un contador que inicia en 0
do {
console.log(`El número aleatorio es: ${getRandomNumber(200)}`)
counter++; // Increment operator
} while (counter < 10);
Similar to using the addition assignment operator and adding 1 , the increment operator adds 1 to the current number value stored in counter and returns a value each time through the loop. The approach you use is up to you; the course will use the increment operator moving forward.

Decrement operator

JavaScript also provides the decrement operator (--), which subtracts one from an integer value. For example:
function getRandomNumber(upper) { ... }

let counter = 10;
do {
console.log( `The random number is ${getRandomNumber(10)}` );
counter--; // decrement operator
} while ( counter > 0 );
This time, the loop counts down from 10 to 0, decreasing the value assigned to the counter variable by one each time through the loop. It's the same as counter -= 1


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.