Features
- Validates input to ensure only numbers are entered.
- Handles cases where min is greater than max by swapping values.
- Generates a random integer between the specified min and max values.
Basic Arithmetic and Logical Operations
Random number generation
Note:
The increment (++) and decrement (--) operators can be used as prefix and postfix.
Prefix: (++x or --x), the variable is modified before its value is used in an expression.
Postfix: (x++ or x--), the variable value is used in the expression before it is modified.
Note:The difference between loose (==) and strict (===) equality is that strict equality checks the type as well as the value.
For example,
5 == '5'is true because the string '5' is parsed to the number 5, but5 === '5'is false because they are of different types.
Conditional statements
The ternary operator is a shorthand for an if-else statement. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.
condition ? expressionIfTrue : expressionIfFalse;
If you want only the if statement without an else, you can use the ternary operator with an empty expression for the false case.
condition ? expressionIfTrue : null; or condition && expressionIfTrue;
In the second example, if the condition is true, the expression will be executed. If the condition is false, the expression will not be executed, and the result will be false.
The switch statement is used to perform different actions based on different conditions.
switch (expression) {
case value1:
// code to be executed if expression === value1
break;
case value2:
// code to be executed if expression === value2
break;
default:
// code to be executed if expression doesn't match any case
}
The setTimeout function is used to execute a function after a specified delay, while setInterval is used to execute a function repeatedly at specified intervals.
setTimeout(function, delay); setInterval(function, interval);
Both functions return a unique identifier that can be used to cancel the timeout or interval using clearTimeout or clearInterval.
// Example of setTimeout
const timeoutId = setTimeout(() => {
console.log('This will be logged after 2 seconds');
}, 2000);
// Example of setInterval
const intervalId = setInterval(() => {
console.log('This will be logged every 2 seconds');
}, 2000);
// To cancel the timeout
clearTimeout(timeoutId);
// To cancel the interval
clearInterval(intervalId);
it works with by calling