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 a countdown using ↴
recursion programming technique where a function calls itself repeatedly to solve a problem.
setTimeout() → method sets a timer which executes a function or specified piece of code once the timer expires.
Recursion The act of a function calling itself.
Recursion is used to solve problems that contain smaller sub-problems.
A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).
Use recursion to find the factorial of 5.
let x = 5;
function factorial(num) {
if (num > 1) { Recursive call
return num * factorial(num - 1);
}
else { Base case
return 1;
};
}
call function
factorial(x); returns ↴
120 factorial of 5 → 120
setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
It is commonly used to call a function that is executed just once, after a delay.
syntax
setTimeout(function, delay);
console.log("Start");
setTimeout(function() {
console.log("Middle");
}, 1000); → delay in milliseconds
console.log("End");
returns ↴
Start → printed to console
End → printed to console
Middle → printed to console after 1000 ms
The return value of the function is an timeout ID which can be used later to cancel the timer if needed.
clearTimeout() method cancels a timeout previously established by calling setTimeout().
If the parameter provided does not identify a previously established action, this method does nothing.
syntax
clearTimeout(timeoutID);
timeout ID → identifier of the action you want to cancel. This ID was returned by the corresponding call to setTimeout()
Initialize a variable to hold the number to start countdown from.
const countFrom = 5; → user input
Initialize a variable to hold the delay interval.
const delay = 500; → user input
Initialize a variable to hold the message to display.
const message = "BLAST OFF"; → user input
Define a function countdown to countdown to zero from a given number, at a given interval duration and a message to display.
function countdown(count) {}
The function takes a single parameter count the starting count for the countdown.
Starting count for the countdown.
const countFrom = 5 countFrom
Interval duration in milliseconds.
const delay = 500 delay
Message to display after countdown.
const message = "BLAST OFF" message
Base case
If count is less than 0 display the message
if (count < 0) {
console.log(message)
return end execution of function
}
Recursive call
If count is 0 or greater, log the current count to the console.
console.log(count)
Set a timeout to call the countdown function again after the specified delay.
The countdown function calls itself with a decremented value, creating a loop until the base case is met.
setTimeout(() => countdown(count - 1), delay)
Call the function with ↴
countdown(countFrom);
Generate a countdown.
const countFrom = 5;
const delay = 500;
const message = "BLAST OFF";
function countdown(count) {
if (count < 0) {
console.log(message);
return;
}
console.log(count);
setTimeout(() => countdown(count - 1), delay);
}
call function
countdown(countFrom); returns ↴
5 → printed to console after 500 ms
4 → printed to console after 500 ms
3 → printed to console after 500 ms
2 → printed to console after 500 ms
1 → printed to console after 500 ms
0 → printed to console after 500 ms
BLAST OFF → printed to console after 500 ms