From 973f76bf0d94ed8f43d1d38ad838d1f14b0948b5 Mon Sep 17 00:00:00 2001 From: "Arcadio R." Date: Sun, 9 Nov 2025 13:37:17 +0000 Subject: [PATCH 1/3] added recursion entry --- .../concepts/recursion/recursion.md | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 content/javascript/concepts/recursion/recursion.md diff --git a/content/javascript/concepts/recursion/recursion.md b/content/javascript/concepts/recursion/recursion.md new file mode 100644 index 00000000000..ca621ae6ef1 --- /dev/null +++ b/content/javascript/concepts/recursion/recursion.md @@ -0,0 +1,87 @@ +--- +Title: 'Recursion' +Description: 'Recursion is a way of repeating tasks that rely on a condition to stop' +Subjects: + - 'Web Development' + - 'Computer Science' +Tags: + - 'Recursion' + - 'Functions' + - 'Loops' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' +--- + +Recursion is a way to solve problems that involve repeating a task a certain number of times with a specific condition in mind. When that condition is met, the repetition stops. Very often, it's implemented in the form of recursive functions. + +## Recursive function + +The `recursive function` will repeat a task until a certain defined `condition` is met. Its main characteristic is that it calls itself a number of times to reiterate the task, considering this predetermined condition. When the condition is met, the function stops running, and the recursive function achieves its goal. + +This `condition` is needed for the function to be done. Typically, this is done in the first step of the function. Being the condition missing, the function would run forever, causing an error or crash. + +The `recursive call` is also needed to repeat the task until the condition is met. The function calls itself a number of times. Typically, this is done towards the end of the function. + +## Syntax + +```javascript +function recursiveFunction() { + // Code block to be executed + recursiveFunction() +} +``` + +## Example + +```js +let count = 0; //step 1 + +function countToTen() { + if (count < 10) { // step 2 + count = count+1; //step 3 + console.log(count); //step 3 + countToTen(); //step 4 + } +} + +countToTen(); //step 5 +``` + +The output would be: + +```shell +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +Explanation: + +-1) Set the variable to work with (out of the function). +-2) Decide what condition should be met. +-3) Increase the count by 1 in each function call and output the result. +-4) Repeat the function (recursive call) until the condition is met (in this case: count less than 10). +-5) Run or call the function. + +This method of iterating can be compared to a Loop, another form of recursion. The main difference is that the Loop doesn't have to call itself; it's done using other algorithm structures. For detailed information and application, check the "Loops" entry. + +## Codebyte + +```codebyte/javascript +let count = 0; //step 1 + +function countToTen() { + if (count < 10) { // step 2 + count = count+1; //step 3 + console.log(count); //step 3 + countToTen(); //step 4 + } +} \ No newline at end of file From d357504c22c6a063288bc94b459a981c34e902eb Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 10 Nov 2025 13:33:45 +0530 Subject: [PATCH 2/3] minor content fixes --- .../concepts/recursion/recursion.md | 71 ++++++++++--------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/content/javascript/concepts/recursion/recursion.md b/content/javascript/concepts/recursion/recursion.md index ca621ae6ef1..94d7f4c153f 100644 --- a/content/javascript/concepts/recursion/recursion.md +++ b/content/javascript/concepts/recursion/recursion.md @@ -1,51 +1,52 @@ --- Title: 'Recursion' -Description: 'Recursion is a way of repeating tasks that rely on a condition to stop' +Description: 'Recursion is a technique where a function calls itself to solve smaller instances of the same problem.' Subjects: - - 'Web Development' - 'Computer Science' + - 'Web Development' Tags: - - 'Recursion' - 'Functions' - 'Loops' + - 'Recursion' CatalogContent: - 'introduction-to-javascript' - 'paths/front-end-engineer-career-path' --- -Recursion is a way to solve problems that involve repeating a task a certain number of times with a specific condition in mind. When that condition is met, the repetition stops. Very often, it's implemented in the form of recursive functions. - -## Recursive function +**Recursion** is a technique where a function calls itself to solve smaller instances of the same problem until a specific condition called the base case is met. It’s commonly used when a task can be broken down into repeated sub-tasks. -The `recursive function` will repeat a task until a certain defined `condition` is met. Its main characteristic is that it calls itself a number of times to reiterate the task, considering this predetermined condition. When the condition is met, the function stops running, and the recursive function achieves its goal. +A recursive function must always have two parts: -This `condition` is needed for the function to be done. Typically, this is done in the first step of the function. Being the condition missing, the function would run forever, causing an error or crash. +- **Base case:** Defines when the recursion should stop to prevent infinite calls. +- **Recursive call:** The function calls itself to continue solving the smaller problem until the base case is reached. -The `recursive call` is also needed to repeat the task until the condition is met. The function calls itself a number of times. Typically, this is done towards the end of the function. +If the base case is missing, the function will keep calling itself indefinitely, causing a stack overflow error. ## Syntax -```javascript +```pseudo function recursiveFunction() { - // Code block to be executed - recursiveFunction() + // Code block to execute + recursiveFunction(); // Recursive call } ``` ## Example +In this example, the function prints numbers from 1 to 10 by calling itself until a condition is met: + ```js -let count = 0; //step 1 +let count = 0; function countToTen() { - if (count < 10) { // step 2 - count = count+1; //step 3 - console.log(count); //step 3 - countToTen(); //step 4 + if (count < 10) { + count = count + 1; + console.log(count); + countToTen(); // Recursive call } } - -countToTen(); //step 5 + +countToTen(); ``` The output would be: @@ -63,25 +64,29 @@ The output would be: 10 ``` -Explanation: +Here’s what happens step by step: + +- A variable `count` is initialized outside the function. +- The function checks if `count` is less than 10. +- If true, it increments `count`, prints it, and calls itself again. +- When `count` reaches 10, the base case condition fails, and recursion stops. --1) Set the variable to work with (out of the function). --2) Decide what condition should be met. --3) Increase the count by 1 in each function call and output the result. --4) Repeat the function (recursive call) until the condition is met (in this case: count less than 10). --5) Run or call the function. +This process is similar to how loops work, but recursion uses function calls instead of loop structures. -This method of iterating can be compared to a Loop, another form of recursion. The main difference is that the Loop doesn't have to call itself; it's done using other algorithm structures. For detailed information and application, check the "Loops" entry. +## Codebyte Example -## Codebyte +In this example, the function repeatedly calls itself to print numbers from 1 to 10, stopping once the base case condition (`count < 10`) is no longer true: ```codebyte/javascript -let count = 0; //step 1 +let count = 0; function countToTen() { - if (count < 10) { // step 2 - count = count+1; //step 3 - console.log(count); //step 3 - countToTen(); //step 4 + if (count < 10) { + count++; + console.log(count); + countToTen(); } -} \ No newline at end of file +} + +countToTen(); +``` From 574a971f7a3cf739bdb0f2a8e3a6afee7c57b04a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 10 Nov 2025 13:35:48 +0530 Subject: [PATCH 3/3] added backlink --- content/javascript/concepts/recursion/recursion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/javascript/concepts/recursion/recursion.md b/content/javascript/concepts/recursion/recursion.md index 94d7f4c153f..19d8196e0af 100644 --- a/content/javascript/concepts/recursion/recursion.md +++ b/content/javascript/concepts/recursion/recursion.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/front-end-engineer-career-path' --- -**Recursion** is a technique where a function calls itself to solve smaller instances of the same problem until a specific condition called the base case is met. It’s commonly used when a task can be broken down into repeated sub-tasks. +**Recursion** is a technique where a [function](https://www.codecademy.com/resources/docs/javascript/functions) calls itself to solve smaller instances of the same problem until a specific condition called the base case is met. It’s commonly used when a task can be broken down into repeated sub-tasks. A recursive function must always have two parts: