Skip to content

Commit 06ea32b

Browse files
committed
feat: add JSDoc exercises and solutions for function documentation
- Introduce new exercises focused on writing JSDoc comments for functions, enhancing documentation skills. - Add corresponding solution files demonstrating proper JSDoc usage for various functions. - Update package-lock.json to include new exercise and solution modules. - Enhance README files to provide context and guidance on JSDoc usage and best practices.
1 parent efa24a9 commit 06ea32b

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# JSDoc Documentation
2+
3+
👨‍💼 Code without documentation is like a recipe without instructions—technically
4+
all the ingredients are there, but good luck figuring out how to use them!
5+
6+
JSDoc is a documentation format that lives in special comments above your
7+
functions. The best part? TypeScript and your IDE understand JSDoc, so your
8+
documentation shows up in tooltips and autocomplete.
9+
10+
```ts
11+
/**
12+
* Calculates the area of a rectangle.
13+
* @param width - The width of the rectangle
14+
* @param height - The height of the rectangle
15+
* @returns The area in square units
16+
* @example
17+
* const area = calculateArea(5, 10)
18+
* console.log(area) // 50
19+
*/
20+
function calculateArea(width: number, height: number): number {
21+
return width * height
22+
}
23+
```
24+
25+
When you hover over `calculateArea` anywhere in your codebase, you'll see this
26+
documentation!
27+
28+
## Common JSDoc Tags
29+
30+
| Tag | Purpose |
31+
| ---------- | ---------------------------------- |
32+
| `@param` | Documents a function parameter |
33+
| `@returns` | Documents what the function returns|
34+
| `@example` | Shows how to use the function |
35+
| `@throws` | Documents errors the function throws|
36+
| `@see` | Links to related documentation |
37+
38+
🐨 Open <InlineFile file="index.ts" /> and add JSDoc comments to each function.
39+
40+
💰 The description goes right after the opening `/**`:
41+
42+
```ts
43+
/**
44+
* This is the description that explains what the function does.
45+
* It can span multiple lines.
46+
* @param name - Description of the parameter
47+
*/
48+
```
49+
50+
📜 [JSDoc Reference](https://jsdoc.app/)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// JSDoc Documentation
2+
// Writing documentation comments that enhance IDE support and code clarity
3+
4+
// 🐨 Add a JSDoc comment above this function that includes:
5+
// - A description of what the function does
6+
// - @param tag documenting the `a` parameter
7+
// - @param tag documenting the `b` parameter
8+
// - @returns tag documenting what the function returns
9+
// 💰 JSDoc comments start with /** and end with */
10+
function add(a: number, b: number): number {
11+
return a + b
12+
}
13+
14+
// 🐨 Add a JSDoc comment for this function that includes:
15+
// - A description
16+
// - @param tag for the `name` parameter
17+
// - @returns tag
18+
// - @example tag showing how to use the function
19+
// 💰 @example tags let you show usage:
20+
// @example
21+
// const result = greet('Alice')
22+
// console.log(result) // "Hello, Alice!"
23+
function greet(name: string): string {
24+
return `Hello, ${name}!`
25+
}
26+
27+
// 🐨 Add a JSDoc comment for this function that includes:
28+
// - A description explaining what the function calculates
29+
// - @param tag for `principal` (the initial amount)
30+
// - @param tag for `rate` (annual interest rate as decimal)
31+
// - @param tag for `years` (number of years)
32+
// - @returns tag explaining the result
33+
// - @example tag showing typical usage
34+
function calculateCompoundInterest(
35+
principal: number,
36+
rate: number,
37+
years: number,
38+
): number {
39+
return principal * Math.pow(1 + rate, years)
40+
}
41+
42+
// 🐨 Create a function called `clamp` that:
43+
// - Takes a value, min, and max
44+
// - Returns the value constrained between min and max
45+
// - Has a complete JSDoc comment with description, @param, @returns, and @example
46+
// 💰 Math.max(min, Math.min(max, value)) will clamp a value
47+
48+
export { add, greet, calculateCompoundInterest }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_05.functions_05.problem.jsdoc",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# JSDoc Documentation
2+
3+
👨‍💼 Excellent work! Your functions now have professional documentation.
4+
5+
🦉 Key takeaways about JSDoc:
6+
7+
1. **IDE Integration** - When you hover over a documented function, you'll see
8+
the description, parameters, return type, and examples right in your editor
9+
2. **Self-Documenting Code** - Good JSDoc makes code easier to understand
10+
without reading the implementation
11+
3. **`@example` is powerful** - Examples show how to actually use the function,
12+
which is often clearer than a description
13+
14+
```ts
15+
/**
16+
* Constrains a value to be within a specified range.
17+
* @param value - The value to clamp
18+
* @param min - The minimum allowed value
19+
* @param max - The maximum allowed value
20+
* @returns The clamped value between min and max
21+
* @example
22+
* clamp(15, 0, 10) // returns 10
23+
* clamp(-5, 0, 10) // returns 0
24+
*/
25+
function clamp(value: number, min: number, max: number): number {
26+
return Math.max(min, Math.min(max, value))
27+
}
28+
```
29+
30+
While TypeScript gives you type information automatically, JSDoc adds the
31+
*human* context: what does this function actually do? When should I use it?
32+
What do the numbers mean?
33+
34+
📝 For your own projects, consider adding JSDoc to:
35+
36+
- Public API functions
37+
- Complex utility functions
38+
- Functions with non-obvious parameter meanings
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import assert from 'node:assert/strict'
2+
import { test } from 'node:test'
3+
import { add, greet, calculateCompoundInterest, clamp } from './index.ts'
4+
5+
await test('add has JSDoc comment', () => {
6+
const source = add.toString()
7+
// Function should work correctly
8+
assert.strictEqual(add(2, 3), 5, '🚨 add(2, 3) should return 5')
9+
assert.strictEqual(add(-1, 1), 0, '🚨 add(-1, 1) should return 0')
10+
})
11+
12+
await test('greet has JSDoc comment and works correctly', () => {
13+
assert.strictEqual(
14+
greet('Alice'),
15+
'Hello, Alice!',
16+
'🚨 greet("Alice") should return "Hello, Alice!"',
17+
)
18+
assert.strictEqual(
19+
greet('World'),
20+
'Hello, World!',
21+
'🚨 greet("World") should return "Hello, World!"',
22+
)
23+
})
24+
25+
await test('calculateCompoundInterest works correctly', () => {
26+
// $1000 at 5% for 10 years = $1000 * (1.05)^10 ≈ $1628.89
27+
const result = calculateCompoundInterest(1000, 0.05, 10)
28+
assert.ok(
29+
Math.abs(result - 1628.89) < 0.1,
30+
`🚨 calculateCompoundInterest(1000, 0.05, 10) should be approximately 1628.89, got ${result}`,
31+
)
32+
33+
// $100 at 10% for 1 year = $110
34+
const simpleResult = calculateCompoundInterest(100, 0.1, 1)
35+
assert.ok(
36+
Math.abs(simpleResult - 110) < 0.01,
37+
`🚨 calculateCompoundInterest(100, 0.1, 1) should be approximately 110, got ${simpleResult}`,
38+
)
39+
})
40+
41+
await test('clamp constrains values correctly', () => {
42+
// Value above max should return max
43+
assert.strictEqual(
44+
clamp(15, 0, 10),
45+
10,
46+
'🚨 clamp(15, 0, 10) should return 10 (value above max)',
47+
)
48+
49+
// Value below min should return min
50+
assert.strictEqual(
51+
clamp(-5, 0, 10),
52+
0,
53+
'🚨 clamp(-5, 0, 10) should return 0 (value below min)',
54+
)
55+
56+
// Value within range should return unchanged
57+
assert.strictEqual(
58+
clamp(5, 0, 10),
59+
5,
60+
'🚨 clamp(5, 0, 10) should return 5 (value within range)',
61+
)
62+
63+
// Edge cases
64+
assert.strictEqual(
65+
clamp(0, 0, 10),
66+
0,
67+
'🚨 clamp(0, 0, 10) should return 0 (value at min)',
68+
)
69+
assert.strictEqual(
70+
clamp(10, 0, 10),
71+
10,
72+
'🚨 clamp(10, 0, 10) should return 10 (value at max)',
73+
)
74+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// JSDoc Documentation
2+
// Writing documentation comments that enhance IDE support and code clarity
3+
4+
/**
5+
* Adds two numbers together.
6+
* @param a - The first number to add
7+
* @param b - The second number to add
8+
* @returns The sum of a and b
9+
*/
10+
function add(a: number, b: number): number {
11+
return a + b
12+
}
13+
14+
/**
15+
* Creates a greeting message for a person.
16+
* @param name - The name of the person to greet
17+
* @returns A greeting string
18+
* @example
19+
* const result = greet('Alice')
20+
* console.log(result) // "Hello, Alice!"
21+
*/
22+
function greet(name: string): string {
23+
return `Hello, ${name}!`
24+
}
25+
26+
/**
27+
* Calculates compound interest on a principal amount.
28+
* @param principal - The initial amount of money
29+
* @param rate - The annual interest rate as a decimal (e.g., 0.05 for 5%)
30+
* @param years - The number of years to compound
31+
* @returns The final amount after compound interest
32+
* @example
33+
* const result = calculateCompoundInterest(1000, 0.05, 10)
34+
* console.log(result) // 1628.89 (approximately)
35+
*/
36+
function calculateCompoundInterest(
37+
principal: number,
38+
rate: number,
39+
years: number,
40+
): number {
41+
return principal * Math.pow(1 + rate, years)
42+
}
43+
44+
/**
45+
* Constrains a value to be within a specified range.
46+
* @param value - The value to clamp
47+
* @param min - The minimum allowed value
48+
* @param max - The maximum allowed value
49+
* @returns The clamped value between min and max
50+
* @example
51+
* clamp(15, 0, 10) // returns 10
52+
* clamp(-5, 0, 10) // returns 0
53+
* clamp(5, 0, 10) // returns 5
54+
*/
55+
function clamp(value: number, min: number, max: number): number {
56+
return Math.max(min, Math.min(max, value))
57+
}
58+
59+
// Test the functions
60+
console.log(add(2, 3)) // 5
61+
console.log(greet('World')) // "Hello, World!"
62+
console.log(calculateCompoundInterest(1000, 0.05, 10)) // ~1628.89
63+
console.log(clamp(15, 0, 10)) // 10
64+
65+
export { add, greet, calculateCompoundInterest, clamp }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_05.functions_05.solution.jsdoc",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)