Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Generate random colors using ↴
for loop → executes a block of code a number of times.
Math.random() static method → returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1.
Math.floor() static method → always rounds down and returns the largest integer less than or equal to a given number.
for loop repeatedly executes a block of code until a specified condition evaluates to false.
The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.
for (let x = 0; x < 4; x++) {
console.log(x);
}
Loop variable x is initialized to 0
Condition x < 4 is checked before each iteration.
The loop will continue to run as long as x is less than 4
The loop repeatedly executes a block of code 4 times, from 0 to 3
For each iteration of the loop, the current value of x is printed to the console.
After each iteration, x is incremented by 1 x++
When x reaches 4 the condition evaluates to false, terminating the loop.
0
1
2
3 → printed to console
Random number generation process of generating a number that is not predictable.
Math.random() static method returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive), with approximately uniform distribution over that range, which can be scaled to a desired range.
Math.random() always returns a number lower than 1
Math.random(); → 0.7409774889800926
Math.random(); → 0.5533596609238658
Math.random(); → 0.26768267226253617
Returns random numbers between 0 (inclusive) and 1 (exclusive)
Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
Math.floor(5.25); → 5
Math.floor(2.99); → 2
Math.floor(6.55); → 6
Rounds down and returns the largest integer less than or equal to a given number.
Define a function randomColor to generate a random color in hexadecimal color code.
function randomColor() {}
The function generates a random hexadecimal color code.
Initialize string containing all possible hexadecimal characters.
const letters = "0123456789ABCDEF" letters
Initialize the color string with a hash symbol #
let color = "#" color
Loop 6 times to create a six-character color code.
for (let x = 0; x < 6; x++) {}
Within the loop, a random index is generated to select a character from the letters string.
Append a random character from the letters string to the color string.
color += letters[Math.floor(Math.random() * 16)]
Return the generated color code.
return color
Generate random color.
function randomColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let x = 0; x < 6; x++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
call function
randomColor(); → #ab4351
randomColor(); → #b48a2e
randomColor(); → #769e96
Function returns a random color after each call.