Skip to content

Commit 58c2812

Browse files
authored
Merge branch 'exercism:main' into update-tests-diffie-hellman
2 parents 7bc0075 + 1637e85 commit 58c2812

File tree

432 files changed

+17843
-1797
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

432 files changed

+17843
-1797
lines changed

.github/workflows/action-format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
run: corepack enable pnpm
6565

6666
- name: Use Node.js LTS (22.x)
67-
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b
67+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
6868
with:
6969
node-version: 22.x
7070
cache: 'pnpm'

.github/workflows/ci.js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
run: corepack enable pnpm
1818

1919
- name: Use Node.js LTS (22.x)
20-
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b
20+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
2121
with:
2222
node-version: 22.x
2323
cache: 'pnpm'
@@ -41,7 +41,7 @@ jobs:
4141
run: corepack enable pnpm
4242

4343
- name: Use Node.js ${{ matrix.node-version }}
44-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af
44+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
4545
with:
4646
node-version: ${{ matrix.node-version }}
4747
cache: 'pnpm'

.github/workflows/pr.ci.js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: corepack enable pnpm
2929

3030
- name: Use Node.js LTS (22.x)
31-
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b
31+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
3232
with:
3333
node-version: 22.x
3434
cache: 'pnpm'
@@ -65,7 +65,7 @@ jobs:
6565
run: corepack enable pnpm
6666

6767
- name: Use Node.js ${{ matrix.node-version }}
68-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af
68+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
6969
with:
7070
node-version: ${{ matrix.node-version }}
7171
cache: 'pnpm'

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
presets: [['@exercism/babel-preset-javascript', { corejs: '3.39' }]],
2+
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
33
plugins: [],
44
};

concepts/basics/about.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ While it is most well-known as the scripting language for Web pages, many non-br
66

77
## (Re-)Assignment
88

9-
There are a few primary ways to assign values to names in JavaScript - using variables or constants. On Exercism, variables are always written in [camelCase][wiki-camel-case]; constants are written in [SCREAMING_SNAKE_CASE][wiki-snake-case]. There is no official guide to follow, and various companies and organizations have various style guides. _Feel free to write variables any way you like_. The upside from writing them the way the exercises are prepared is that they'll be highlighted differently in the web interface and most IDEs.
9+
There are a few primary ways to assign values to names in JavaScript - using variables or constants. On Exercism, variables are always written in [camelCase][wiki-camel-case]; constants are written in [SCREAMING_SNAKE_CASE][wiki-snake-case]. There is no official guide to follow, and various companies and organizations have various style guides. _Feel free to write variables any way you like_. The upside to writing them the way the exercises are prepared is that they'll be highlighted differently in the web interface and most IDEs.
1010

1111
Variables in JavaScript can be defined using the [`const`][mdn-const], [`let`][mdn-let] or [`var`][mdn-var] keyword.
1212

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"blurb": "Recursive functions are functions that call themselves.",
3-
"authors": ["SleeplessByte"],
3+
"authors": ["SleeplessByte", "zynthatrix"],
44
"contributors": []
55
}

concepts/recursion/about.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
# About
1+
# Understanding Recursion in JavaScript
2+
3+
Recursion is a powerful concept in programming that involves a function calling itself.
4+
It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems.
5+
We'll explore recursion in JavaScript with easy-to-understand examples.
6+
7+
## What is Recursion?
8+
9+
Recursion occurs when a function calls itself, either directly or indirectly.
10+
It's similar to a loop, but it may involve breaking a problem down into smaller, more manageable sub-problems.
11+
12+
### Example 1: Countdown
13+
14+
Let's start with a simple example: a countdown function.
15+
16+
```javascript
17+
function countdown(num) {
18+
// Base case
19+
if (num <= 0) {
20+
console.log('Blastoff!');
21+
return;
22+
}
23+
24+
// Recursive case
25+
console.log(num);
26+
countdown(num - 1);
27+
}
28+
29+
// Call the function
30+
countdown(5);
31+
```
32+
33+
In this example:
34+
35+
- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
36+
- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`.
37+
38+
### Example 2: Factorial
39+
40+
Now, let's look at a classic example of recursion: calculating the factorial of a number.
41+
42+
```javascript
43+
function factorial(n) {
44+
// Base case
45+
if (n === 0 || n === 1) {
46+
return 1;
47+
}
48+
49+
// Recursive case
50+
return n * factorial(n - 1);
51+
}
52+
53+
// Test the function
54+
console.log(factorial(5)); // Output: 120
55+
```
56+
57+
In this example:
58+
59+
- **Base case**: When `n` is 0 or 1, the function returns 1.
60+
- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`.
61+
62+
## Key Concepts
63+
64+
### Base Case
65+
66+
Every recursive function should have at least one base case, a condition where the function stops calling itself.
67+
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
68+
69+
### Recursive Case
70+
71+
The recursive case defines how the function calls itself with a smaller or simpler version of the problem.
72+
73+
## Pros and Cons of Recursion
74+
75+
**Pros:**
76+
77+
- Elegant solution for certain problems.
78+
- Mimics the mathematical induction concept.
79+
80+
**Cons:**
81+
82+
- Can be less efficient than iterative solutions.
83+
- May lead to stack overflow for deep recursion.
84+
85+
## Conclusion
86+
87+
Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems.
88+
Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.
89+
90+
**Learn More:**
91+
92+
- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion)
93+
- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)

concepts/recursion/introduction.md

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,93 @@
1-
# Introduction
1+
# Understanding Recursion in JavaScript
22

3-
The ability for something to be defined in terms of itself is called recursion.
4-
Recursive functions are functions that call themselves.
3+
Recursion is a powerful concept in programming that involves a function calling itself.
4+
It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems.
5+
We'll explore recursion in JavaScript with easy-to-understand examples.
56

6-
Suppose that you have a function called `recurse`.
7-
This function is recursive if it calls itself inside its body, like this:
7+
## What is Recursion?
88

9-
```js
10-
function recurse() {
11-
// ...
12-
recurse();
13-
// ...
14-
}
15-
```
9+
Recursion occurs when a function calls itself, either directly or indirectly.
10+
It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems.
1611

17-
A recursive function usually has a condition to stop calling itself and return a value, known as a _base case_.
18-
If a base case is missing, in most cases, because it will call itself indefinitely, it would be able to run forever.
19-
In reality, in most of those situations, you'll end up with a "StackSize error": an error raised by the runtime because the _stack_ of function calls has grown beyond a predefined limit because each recursive call adds to this _stack_ until it returns (and it doesn't).
20-
The message of this error is `Maximum call stack size exceeded`.
21-
22-
```js
23-
function recurse() {
24-
if (baseCondition) {
25-
// stop calling itself
26-
//...
27-
} else {
28-
recurse();
29-
}
30-
}
31-
```
12+
### Example 1: Countdown
3213

33-
Recursive functions often can be used instead of `for` loops for more succinct code.
34-
For example, take a countdown.
35-
Here's the more intuitive `for` loop approach:
14+
Let's start with a simple example: a countdown function.
3615

37-
```js
38-
function countDown(fromNumber) {
39-
for (let i = fromNumber; i > 0; i--) {
40-
console.log(i);
16+
```javascript
17+
function countdown(num) {
18+
// Base case
19+
if (num <= 0) {
20+
console.log('Blastoff!');
21+
return;
4122
}
23+
24+
// Recursive case
25+
console.log(num);
26+
countdown(num - 1);
4227
}
4328

44-
countDown(3); // 3, 2, 1 in separate lines
29+
// Call the function
30+
countdown(5);
4531
```
4632

47-
We could solve this using recursion too:
33+
In this example:
34+
35+
- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
36+
- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`.
4837

49-
```js
50-
function countDown(fromNumber) {
51-
console.log(fromNumber);
52-
if (fromNumber > 1) {
53-
countDown(fromNumber - 1);
38+
### Example 2: Factorial
39+
40+
Now, let's look at a classic example of recursion: calculating the factorial of a number.
41+
42+
```javascript
43+
function factorial(n) {
44+
// Base case
45+
if (n === 0 || n === 1) {
46+
return 1;
5447
}
48+
49+
// Recursive case
50+
return n * factorial(n - 1);
5551
}
5652

57-
countDown(3); // same result
53+
// Test the function
54+
console.log(factorial(5)); // Output: 120
5855
```
5956

60-
Here, our base case is when `fromNumber` is 1, in which case we don't call `countDown` again.
57+
In this example:
6158

62-
Apart from just displaying numbers, recursive functions can be used for more complicated procedures, such as keeping a sum or total.
59+
- **Base case**: When `n` is 0 or 1, the function returns 1.
60+
- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`.
6361

64-
```js
65-
function sum(n) {
66-
if (n <= 1) {
67-
return n;
68-
}
69-
return n + sum(n - 1);
70-
}
62+
## Key Concepts
7163

72-
sum(3); // 6
73-
```
64+
### Base Case
65+
66+
Every recursive function should have at least one base case, a condition where the function stops calling itself.
67+
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
68+
69+
### Recursive Case
70+
71+
The recursive case defines how the function calls itself with a smaller or simpler version of the problem.
72+
73+
## Pros and Cons of Recursion
74+
75+
**Pros:**
76+
77+
- Elegant solution for certain problems.
78+
- Mimics the mathematical induction concept.
79+
80+
**Cons:**
81+
82+
- Can be less efficient than iterative solutions.
83+
- May lead to stack overflow for deep recursion.
84+
85+
## Conclusion
86+
87+
Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems.
88+
Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.
89+
90+
**Learn More:**
91+
92+
- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion)
93+
- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)

0 commit comments

Comments
 (0)