Skip to content

Commit 09fbe03

Browse files
committed
upload lesson code
0 parents  commit 09fbe03

File tree

8 files changed

+1828
-0
lines changed

8 files changed

+1828
-0
lines changed

.gitignore

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
2+
3+
# Created by https://www.gitignore.io/api/visualstudiocode,windows,node
4+
# Edit at https://www.gitignore.io/?templates=visualstudiocode,windows,node
5+
6+
### Node ###
7+
# Logs
8+
logs
9+
*.log
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
14+
# Runtime data
15+
pids
16+
*.pid
17+
*.seed
18+
*.pid.lock
19+
20+
# Directory for instrumented libs generated by jscoverage/JSCover
21+
lib-cov
22+
23+
# Coverage directory used by tools like istanbul
24+
coverage
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# TypeScript v1 declaration files
46+
typings/
47+
48+
# Optional npm cache directory
49+
.npm
50+
51+
# Optional eslint cache
52+
.eslintcache
53+
54+
# Optional REPL history
55+
.node_repl_history
56+
57+
# Output of 'npm pack'
58+
*.tgz
59+
60+
# Yarn Integrity file
61+
.yarn-integrity
62+
63+
# dotenv environment variables file
64+
.env
65+
66+
# parcel-bundler cache (https://parceljs.org/)
67+
.cache
68+
69+
# next.js build output
70+
.next
71+
72+
# nuxt.js build output
73+
.nuxt
74+
75+
# vuepress build output
76+
.vuepress/dist
77+
78+
# Serverless directories
79+
.serverless
80+
81+
# FuseBox cache
82+
.fusebox/
83+
84+
### VisualStudioCode ###
85+
.vscode/*
86+
!.vscode/settings.json
87+
!.vscode/tasks.json
88+
!.vscode/launch.json
89+
!.vscode/extensions.json
90+
91+
### VisualStudioCode Patch ###
92+
# Ignore all local history of files
93+
.history
94+
95+
### Windows ###
96+
# Windows thumbnail cache files
97+
Thumbs.db
98+
ehthumbs.db
99+
ehthumbs_vista.db
100+
101+
# Dump file
102+
*.stackdump
103+
104+
# Folder config file
105+
[Dd]esktop.ini
106+
107+
# Recycle Bin used on file shares
108+
$RECYCLE.BIN/
109+
110+
# Windows Installer files
111+
*.cab
112+
*.msi
113+
*.msix
114+
*.msm
115+
*.msp
116+
117+
# Windows shortcuts
118+
*.lnk
119+
120+
# End of https://www.gitignore.io/api/visualstudiocode,windows,node
121+
122+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
123+
.vscode/
124+
.eslintrc.json

00_basicFunctions.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// function declaration
3+
function func(a, b) {
4+
return a+b;
5+
}
6+
7+
// variable storing a function
8+
// What is a a key difference?
9+
const varFunc = function (a,b) {
10+
return a+b;
11+
}
12+
13+
console.log(func(2,2));
14+
console.log(varFunc(2,2));
15+
16+
// immediately invoked function expression
17+
console.log (
18+
(function (a,b) {
19+
return a + b;
20+
})(2,2)
21+
);
22+
23+
24+
25+
26+
27+

01_functionalProgramming.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Person {
2+
constructor(name, age) {
3+
this.name = name;
4+
this.age = age;
5+
}
6+
7+
happyBirthday() {
8+
this.age++;
9+
}
10+
}
11+
12+
function happyFunkyBirthday(Obj) {
13+
let newObj = {...Obj}
14+
newObj.age ++;
15+
return newObj;
16+
}
17+
18+
19+
let sally = new Person('Sally', 25);
20+
sally.happyBirthday();
21+
22+
console.log(sally);
23+
24+
const Jose = {
25+
name: 'Jose',
26+
age: 35,
27+
}
28+
29+
console.log(happyFunkyBirthday(Jose));
30+
console.log(Jose);
31+
32+
33+

02_syncCallback.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function executeCallback(string, callback, hasArgs = false) {
2+
console.log(string);
3+
if (hasArgs) {
4+
callback(4,5);
5+
} else {
6+
callback();
7+
}
8+
9+
console.log('\n');
10+
11+
}
12+
13+
function executeCallbackWithArgs(string, callbackWithArgs) {
14+
console.log(string);
15+
callbackWithArgs(1,2);
16+
17+
let a=3;
18+
let b=4;
19+
callbackWithArgs(a,b);
20+
// callbackWithArgs(a,string);
21+
}
22+
23+
24+
function callbackA() {
25+
console.log('Hi, I am callback A!');
26+
}
27+
28+
29+
function driver(input) {
30+
31+
switch (input) {
32+
33+
case 1:
34+
executeCallback('Starting callback A', callbackA);
35+
break;
36+
37+
case 2:
38+
executeCallback('Starting anonymous callback :O', function () {
39+
console.log('Hi, I am an anonymous callback');
40+
});
41+
break;
42+
43+
case 3:
44+
executeCallback('Starting anonymous callback that does math :O :O', function (a, b) {
45+
console.log('Hi, I am an anonymous callback that can DO MATH :D');
46+
console.log (`${a} + ${b} = ${a+b}`);
47+
}, true);
48+
break;
49+
50+
case 4:
51+
executeCallback('Starting another anonymous callback that does math :O :O', () => function (a, b) {
52+
console.log('Hi, I am an anonymous callback that can DO MATH :D');
53+
console.log(`${a} + ${b} = ${a + b}`);
54+
}(6,7));
55+
break;
56+
57+
case 5:
58+
//passing arguments into callbacks
59+
executeCallbackWithArgs('Starting callback with args!', function (a,b) {
60+
console.log('Hi, I am an anonymous callback with arguments!');
61+
console.log(`My first argument is ${a}`);
62+
console.log(`My second argument is ${b}`);
63+
});
64+
break;
65+
66+
default:
67+
console.log('Please provide valid input!');
68+
69+
}
70+
}
71+
72+
let arg = process.argv[2];
73+
console.log(arg);
74+
driver(Number(arg) || null);
75+

03_asyncCallbacks.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function exampleA() {
2+
3+
function asyncCallbackFunc(a, b) {
4+
console.log('...');
5+
console.log('Whew! I finally get to Speak!!!');
6+
console.log(`I was given ${a} and ${b}`);
7+
}
8+
9+
10+
11+
console.log('I will speak first :O');
12+
console.log('I will speak first too :O');
13+
console.log('I will speak first TOO :O');
14+
console.log('ME TOOO :O');
15+
console.log('ME THREEEEEEEE :O');
16+
17+
setTimeout(function () {
18+
asyncCallbackFunc('Hello', 'World');
19+
}, 3000);
20+
}
21+
22+
function exampleB(isOrdered) {
23+
24+
function A() {
25+
console.log('A');
26+
}
27+
function B() {
28+
console.log('B');
29+
}
30+
function C() {
31+
console.log('C');
32+
}
33+
34+
// what determines the order of this?
35+
function unordered(params) {
36+
setTimeout(A, 2000);
37+
setTimeout(B, 3000);
38+
setTimeout(C, 1000);
39+
}
40+
41+
function ordered() {
42+
setTimeout(() => {
43+
A();
44+
setTimeout(() => {
45+
B();
46+
setTimeout(() => {
47+
C();
48+
}, 1000);
49+
}, 4000);
50+
}, 3000);
51+
}
52+
53+
if (isOrdered) {
54+
ordered();
55+
} else {
56+
unordered();
57+
}
58+
59+
60+
}
61+
62+
const arg = Number(process.argv[2]);
63+
const isOrdered = Boolean(process.argv[3]);
64+
65+
(arg === 1) ? exampleA() : exampleB(isOrdered);
66+
// exampleA();
67+
// exampleB(false);
68+
69+

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# DCI - Lesson on Callbacks
2+
3+
## Contact
4+
5+
Tanzim Mokammel
6+
7+
mtanzim@gmail.com
8+
9+
## Lecture Notes
10+
11+
Please find notes [here]().
12+
13+
## Further Reading
14+
15+
Below links contain additional information.
16+
17+
- [React Weather App Example](https://github.com/mtanzim/react-weather-app)

0 commit comments

Comments
 (0)