Skip to content

Commit 9efcc1b

Browse files
Adding recursive function (#177)
* adding continue.md in en lang * adding recursive fxns page
1 parent ecc7037 commit 9efcc1b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Recursive Functions in JavaScript
3+
description: An explanation of recursive functions in JavaScript.
4+
---
5+
6+
# Recursive Functions in JavaScript
7+
8+
In JavaScript, a recursive function is a function that calls itself in order to solve a problem. Recursion is a powerful concept that can be used to solve complex problems by breaking them down into smaller, more manageable subproblems. This document provides an overview of recursive functions in JavaScript, their syntax, common use cases, and best practices.
9+
10+
## Syntax
11+
12+
A recursive function typically has the following structure:
13+
14+
```javascript
15+
function recursiveFunction(params) {
16+
// Base case: the simplest scenario
17+
if (/* base case condition */) {
18+
// return a value or perform an action
19+
} else {
20+
// Recursive case: call the function with modified parameters
21+
return recursiveFunction(modifiedParams);
22+
}
23+
}
24+
```
25+
Common Use Cases
26+
Recursive functions are often used to solve problems that can be divided into smaller, similar subproblems. Here are some common use cases:
27+
28+
1. Calculating Factorials
29+
A recursive function can be used to calculate the factorial of a number.
30+
```javascript
31+
function factorial(n) {
32+
if (n === 0) {
33+
return 1; // Base case
34+
} else {
35+
return n * factorial(n - 1); // Recursive case
36+
}
37+
}
38+
39+
factorial(5); // Returns 120
40+
```
41+
2. Fibonacci Sequence
42+
The Fibonacci sequence can be calculated using recursion.
43+
```javascript
44+
function fibonacci(n) {
45+
if (n <= 1) {
46+
return n; // Base cases: F(0) = 0, F(1) = 1
47+
} else {
48+
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
49+
}
50+
}
51+
52+
fibonacci(5); // Returns 5
53+
```
54+
55+
In conclusion, recursive functions are a valuable tool in JavaScript for solving problems that involve repetitive subtasks. When used correctly, they can lead to elegant and efficient solutions.

en/loops/continue.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Continue Statement in JavaScript
3+
description: An explanation of the continue statement in JavaScript with an example.
4+
---
5+
6+
# Continue Statement in JavaScript
7+
8+
The `continue` statement in JavaScript is a control flow statement used to skip the current iteration of a loop and continue with the next iteration. It allows you to bypass specific code within a loop under certain conditions. This document provides an overview of the `continue` statement in JavaScript, its syntax, common use cases, and best practices.
9+
10+
## Syntax
11+
12+
In JavaScript, the `continue` statement is used within loops. The syntax is straightforward:
13+
14+
```javascript
15+
for (let i = 1; i <= 5; i++) {
16+
if (i === 3) {
17+
console.log("Skipping 3.");
18+
continue;
19+
}
20+
console.log(`Current value of i: ${i}`);
21+
}
22+

0 commit comments

Comments
 (0)