Skip to content

Commit 4bedfd7

Browse files
jdwilkin4Ksound22
andauthored
feat(curriculum): add type safe math toolkit workshop to typescript module (freeCodeCamp#66073)
Co-authored-by: Kolade <chrisjay967@gmail.com> Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com>
1 parent 6ee501e commit 4bedfd7

22 files changed

Lines changed: 1333 additions & 0 deletions

client/i18n/locales/english/intro.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6187,6 +6187,12 @@
61876187
"In this workshop, you will practice working with type annotations, array types, object types and more by building out a user profile."
61886188
]
61896189
},
6190+
"workshop-type-safe-math-toolkit": {
6191+
"title": "Build a Type Safe Math Toolkit",
6192+
"intro": [
6193+
"In this workshop, you will practice typing functions by building a math toolkit project."
6194+
]
6195+
},
61906196
"lecture-understanding-type-composition": {
61916197
"title": "Understanding Type Composition",
61926198
"intro": [
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
id: 699f1d72e906e4616155290d
3+
title: Step 1
4+
challengeType: 1
5+
dashedName: step-1
6+
---
7+
8+
# --description--
9+
10+
In this workshop, you will continue to learn about type annotations and how they work with functions by building out a type safe math toolkit.
11+
12+
Start by creating a function called `square` that accepts a parameter and returns the square of a number.
13+
14+
# --hints--
15+
16+
You should have a function called `square`.
17+
18+
```js
19+
const explorer = await __helpers.Explorer(code);
20+
assert.exists(explorer.allFunctions.square);
21+
```
22+
23+
Your `square` function should have one parameter.
24+
25+
```js
26+
const explorer = await __helpers.Explorer(code);
27+
assert.lengthOf(explorer.allFunctions.square.parameters, 1);
28+
```
29+
30+
Your `square` function should return the square of a number.
31+
32+
```js
33+
assert.strictEqual(square(2), 4);
34+
assert.strictEqual(square(3), 9);
35+
```
36+
37+
# --seed--
38+
39+
## --seed-contents--
40+
41+
```ts
42+
--fcc-editable-region--
43+
44+
--fcc-editable-region--
45+
```
46+
47+
```json
48+
{
49+
"compilerOptions": {
50+
"noCheck": true
51+
}
52+
}
53+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: 699f3738921fb79a651ded56
3+
title: Step 2
4+
challengeType: 1
5+
dashedName: step-2
6+
---
7+
8+
# --description--
9+
10+
Now it is time to call your function.
11+
12+
Create a variable called `result` and assign it the function call of `square` with an argument of `5`.
13+
14+
Below that, log the value of `result` to the console.
15+
16+
# --before-each--
17+
18+
```js
19+
const spy = __helpers.spyOn(console, 'log');
20+
const getLogs = () => spy.calls.map(call => call?.[0]);
21+
```
22+
23+
# --hints--
24+
25+
You should have a variable called `result`.
26+
27+
```js
28+
assert.isDefined(result);
29+
```
30+
31+
Your `result` variable should be a number.
32+
33+
```js
34+
assert.isNumber(result);
35+
```
36+
37+
Your `result` variable should be the square of `5`.
38+
39+
```js
40+
assert.strictEqual(result, 25);
41+
```
42+
43+
You should log the `result` variable to the console.
44+
45+
```js
46+
assert.equal(getLogs()[0], result);
47+
```
48+
49+
# --seed--
50+
51+
## --seed-contents--
52+
53+
```ts
54+
function square(num) {
55+
return num * num;
56+
}
57+
58+
--fcc-editable-region--
59+
60+
--fcc-editable-region--
61+
```
62+
63+
```json
64+
{
65+
"compilerOptions": {
66+
"noCheck": true
67+
}
68+
}
69+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
id: 699f38937b053e4679878ff1
3+
title: Step 3
4+
challengeType: 1
5+
dashedName: step-3
6+
---
7+
8+
# --description--
9+
10+
When working with vanilla JavaScript, there is nothing to prevent you from calling the `square` function with a string or an array, which would lead to unexpected results.
11+
12+
To illustrate this, change your function call to use the string `"something"` instead of the number `5`.
13+
14+
In the next lesson, you will learn how to use TypeScript to add type safety to your code and prevent these kinds of errors.
15+
16+
# --hints--
17+
18+
Your `square` function call should have an argument of `"something"`.
19+
20+
```js
21+
const explorer = await __helpers.Explorer(code);
22+
assert.isTrue(explorer.variables.result.value.matches("square('something')"));
23+
```
24+
25+
# --seed--
26+
27+
## --seed-contents--
28+
29+
```ts
30+
function square(num) {
31+
return num * num;
32+
}
33+
34+
--fcc-editable-region--
35+
const result = square(5);
36+
--fcc-editable-region--
37+
38+
console.log(result);
39+
```
40+
41+
```json
42+
{
43+
"compilerOptions": {
44+
"noCheck": true
45+
}
46+
}
47+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
id: 699f3cb8d057f29954205d7d
3+
title: Step 4
4+
challengeType: 1
5+
dashedName: step-4
6+
---
7+
8+
# --description--
9+
10+
As you recall from earlier lessons, you can add type annotations to function parameters like this:
11+
12+
```ts
13+
function add(num1: number, num2: number) {
14+
return num1 + num2;
15+
}
16+
```
17+
18+
In this example, TypeScript will display an error if you try to call the `add` function with arguments that are not numbers.
19+
20+
Update your `square` function to include a type annotation for the `num` parameter. The type should be `number`.
21+
22+
# --hints--
23+
24+
You should add a type annotation to the `num` parameter in the `square` function. The type should be a `number`.
25+
26+
```js
27+
const explorer = await __helpers.Explorer(code);
28+
const { parameters } = explorer.allFunctions.square;
29+
assert.isTrue(parameters[0].matches("num: number"));
30+
```
31+
32+
# --seed--
33+
34+
## --seed-contents--
35+
36+
```ts
37+
--fcc-editable-region--
38+
function square(num) {
39+
return num * num;
40+
}
41+
42+
--fcc-editable-region--
43+
const result = square("something");
44+
45+
console.log(result);
46+
```
47+
48+
```json
49+
{
50+
"compilerOptions": {
51+
"noCheck": true
52+
}
53+
}
54+
```
55+
56+
# --solutions--
57+
58+
```ts
59+
function square(num: number) {
60+
return num * num;
61+
}
62+
63+
const result = square("something");
64+
65+
console.log(result);
66+
```
67+
68+
```json
69+
{
70+
"compilerOptions": {
71+
"noCheck": true
72+
}
73+
}
74+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: 699f3f636aa840b7201aa37c
3+
title: Step 5
4+
challengeType: 1
5+
dashedName: step-5
6+
---
7+
8+
# --description--
9+
10+
If you open up the console, you should see the following TypeScript error message:
11+
12+
```md
13+
Argument of type 'string' is not assignable to parameter of type 'number'
14+
```
15+
16+
Now that the parameter is explicitly typed as a number, TypeScript is able to catch the error of trying to pass a string argument to the `square` function.
17+
18+
Change the argument passed to the `square` function to the number `5` to fix the error and see the correct output in the console.
19+
20+
# --hints--
21+
22+
Your `square` function call should have an argument of `5` instead of `"something"`.
23+
24+
```js
25+
assert.strictEqual(result, 25);
26+
```
27+
28+
# --seed--
29+
30+
## --seed-contents--
31+
32+
```ts
33+
function square(num: number) {
34+
return num * num;
35+
}
36+
37+
--fcc-editable-region--
38+
const result = square("something");
39+
--fcc-editable-region--
40+
41+
console.log(result);
42+
```
43+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
id: 699f412114c87221f16a99e3
3+
title: Step 6
4+
challengeType: 1
5+
dashedName: step-6
6+
---
7+
8+
# --description--
9+
10+
Another benefit of using TypeScript is that it will catch errors if you pass to many or to few arguments to a function call.
11+
12+
To illustrate this, update your `square` function call to take two arguments of `5` and `10`.
13+
14+
# --hints--
15+
16+
Your `square` function call should have two arguments of `5` and `10`.
17+
18+
```js
19+
const explorer = await __helpers.Explorer(code);
20+
const { result } = explorer.variables;
21+
assert.isTrue(result.value.matches("square(5, 10)"));
22+
```
23+
24+
# --seed--
25+
26+
## --seed-contents--
27+
28+
```ts
29+
function square(num: number) {
30+
return num * num;
31+
}
32+
33+
--fcc-editable-region--
34+
const result = square(5);
35+
--fcc-editable-region--
36+
37+
console.log(result);
38+
```
39+
40+
```json
41+
{
42+
"compilerOptions": {
43+
"noCheck": true
44+
}
45+
}
46+
```
47+
48+
# --solutions--
49+
50+
```ts
51+
function square(num: number) {
52+
return num * num;
53+
}
54+
55+
const result = square(5, 10);
56+
57+
console.log(result);
58+
```
59+
60+
```json
61+
{
62+
"compilerOptions": {
63+
"noCheck": true
64+
}
65+
}
66+
```

0 commit comments

Comments
 (0)