Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 2ad3064

Browse files
committed
Add step2-01 final folder with solutions
1 parent 5ce0dba commit 2ad3064

File tree

9 files changed

+123
-7
lines changed

9 files changed

+123
-7
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ <h2>Day 2</h2>
8686
<div class="Tile-link">
8787
TypeScript Basics
8888
<div class="Tile-links">
89-
<a target="_blank" href="./step2-01/demo/">demo</a> | <a target="_blank" href="./step2-01/exercise/">exercise</a>
89+
<a target="_blank" href="./step2-01/demo/">demo</a> | <a target="_blank" href="./step2-01/exercise/">exercise</a> | <a target="_blank" href="./step2-01/final/">final</a>
9090
</div>
9191
</div>
9292
</li>

step2-01/demo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Step 2.1 - Introduction to TypeScript (Demo)
22

3-
[Lessons](../../) | [Exercise](../exercise/)
3+
[Lessons](../../) | [Exercise](../exercise/) | [Final](../final/)
44

55
In this step, we'll cover enough TypeScript concepts to be productive with the React & Redux frameworks.
66

step2-01/exercise/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Step 2.1 - Introduction to TypeScript (Exercise)
22

3-
[Lessons](../../) | [Demo](../demo/)
3+
[Lessons](../../) | [Demo](../demo/) | [Final](../final/)
44

55
If you don't already have the app running, start it by running `npm start` from the root of the `frontend-bootcamp` folder.
66

step2-01/exercise/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TODO: import the fib(n) function and the constant from './fibonacci.ts'
2-
// import {fib}, FibConst from ...
2+
// import FibConst, {fib} from ...
33

4-
// TODO: import Stack from './stack.ts'
4+
// TODO: import Stack from ...
55

66
// Do the exercises here, outputting results using console.log()
77
console.log('hello world');
@@ -54,11 +54,10 @@ function makePromise() {
5454

5555
async function run() {
5656
// TODO: call makePromise() using await syntax and log the results
57-
5857
// TODO: call your new async function
5958
}
6059

6160
run();
6261

6362
// Make this file a module so its code doesn't go in the global scope
64-
export { };
63+
export {};

step2-01/final/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Step 2.1 - Introduction to TypeScript (Final)
2+
3+
[Lessons](../../) | [Demo](../demo/) | [Exercise](../exercise/)
4+
5+
Look at the src folder for exercise solutions.

step2-01/final/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="../../assets/step.css" />
5+
</head>
6+
<body class="ms-Fabric">
7+
<div id="markdownReadme" class="exercise" data-src="./README.md"></div>
8+
<div id="app">
9+
Nothing to show here; just look at your console window for output. Hit F12 (<code>cmd+option+I</code> on Mac) to open the console
10+
window.
11+
</div>
12+
<script src="../../assets/scripts.js"></script>
13+
</body>
14+
</html>

step2-01/final/src/fibonacci.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function fib(n: number) {
2+
return n <= 1 ? n : fib(n - 1) + fib(n - 2);
3+
}
4+
5+
const FibConst: number = 3;
6+
export default FibConst;

step2-01/final/src/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import FibConst, { fib } from './fibonacci';
2+
import { Stack } from './stack';
3+
4+
console.log('hello world');
5+
6+
// ---- Modules ----
7+
console.log('fib(FibConst) is:', fib(FibConst));
8+
9+
// ---- Types and Interfaces ----
10+
type TrafficLight = 'red' | 'green' | 'yellow';
11+
const annoyingLight: TrafficLight = 'red';
12+
13+
interface Car {
14+
wheels: number;
15+
color: string;
16+
make: string;
17+
model: string;
18+
}
19+
20+
const myCar: Car = {
21+
wheels: 4,
22+
color: 'blue',
23+
make: 'Toyota',
24+
model: 'Camry'
25+
};
26+
// JSON.stringify makes a nice string representation of an object
27+
console.log('My car:', JSON.stringify(myCar));
28+
29+
// ---- Generics ----
30+
const myStack = new Stack<number>();
31+
myStack.push(1);
32+
myStack.push(2);
33+
myStack.push(3);
34+
console.log('Number on top of the stack:', myStack.pop());
35+
36+
// ---- Spread and Destructuring ----
37+
const obj1 = {
38+
first: 'who',
39+
second: 'what',
40+
third: 'dunno',
41+
left: 'why'
42+
};
43+
44+
const obj2 = {
45+
center: 'because',
46+
pitcher: 'tomorrow',
47+
catcher: 'today'
48+
};
49+
50+
const megaObj = { ...obj1, ...obj2 };
51+
52+
const { first, second, catcher } = megaObj;
53+
console.log('First:', first);
54+
console.log('Second:', second);
55+
console.log('Catcher:', catcher);
56+
57+
// ---- Async / Await ----
58+
function makePromise(): Promise<number> {
59+
return Promise.resolve(5);
60+
}
61+
62+
async function getGreeting(name: string): Promise<string> {
63+
return 'hello ' + name;
64+
}
65+
66+
async function run() {
67+
const result = await makePromise();
68+
console.log('makePromise returned:', result);
69+
70+
const greeting = await getGreeting('Ken');
71+
console.log('greeting:', greeting);
72+
}
73+
74+
run();
75+
76+
// Make this file a module so its code doesn't go in the global scope
77+
export {};

step2-01/final/src/stack.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class Stack<T> {
2+
private _store: T[];
3+
4+
constructor() {
5+
this._store = [];
6+
}
7+
8+
push(elem: T): void {
9+
this._store.push(elem);
10+
}
11+
12+
pop(): T {
13+
return this._store.pop();
14+
}
15+
}

0 commit comments

Comments
 (0)