Skip to content

Commit ad20e17

Browse files
committed
added csbin closures
1 parent d86db2d commit ad20e17

19 files changed

+369
-0
lines changed

csbin/closure/01-createFunction.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function createFunction() {
2+
return function() {
3+
console.log("hello")
4+
}
5+
}
6+
7+
const function1 = createFunction();
8+
function1(); // => should console.log('hello');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function createFunctionPrinter(input) {
2+
return function() {
3+
console.log(input);
4+
}
5+
}
6+
7+
const printSample = createFunctionPrinter('sample');
8+
printSample(); // => should console.log('sample');
9+
const printHello = createFunctionPrinter('hello');
10+
printHello(); // => should console.log('hello');

csbin/closure/03-addByX.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function outer() {
2+
let counter = 0; // this variable is outside incrementCounter's scope
3+
function incrementCounter () {
4+
counter ++;
5+
console.log('counter', counter);
6+
}
7+
return incrementCounter;
8+
}
9+
10+
const willCounter = outer();
11+
const jasCounter = outer();
12+
13+
// Uncomment each of these lines one by one.
14+
// Before your do, guess what will be logged from each function call.
15+
16+
// /*** Uncomment these to check your work! ***/
17+
// willCounter();
18+
// willCounter();
19+
// willCounter();
20+
21+
// jasCounter();
22+
// willCounter();
23+
24+
25+
function addByX(x) {
26+
return function(num) {
27+
return num + x;
28+
}
29+
}
30+
31+
const addByTwo = addByX(2);
32+
console.log(addByTwo(1)); // => should return 3
33+
console.log(addByTwo(2)); // => should return 4
34+
console.log(addByTwo(3)); // => should return 5
35+
36+
const addByThree = addByX(3);
37+
console.log(addByThree(1)); // => should return 4
38+
console.log(addByThree(2)); // => should return 5
39+
40+
const addByFour = addByX(4);
41+
console.log(addByFour(4)); // => should return 8
42+
console.log(addByFour(5)); // => should return 9

csbin/closure/04-once.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function once(func) {
2+
let called = false;
3+
let res;
4+
return function(val) {
5+
if (!called) {
6+
res = func(val);
7+
called = true;
8+
}
9+
return res;
10+
}
11+
}
12+
13+
const addByTwo = num => num + 2;
14+
const onceFunc = once(addByTwo);
15+
console.log(onceFunc(4)); // => should log 6
16+
console.log(onceFunc(10)); // => should log 6
17+
console.log(onceFunc(9001)); // => should log 6

csbin/closure/05-after.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function after(count, func) {
2+
let callCount = 1;
3+
return function() {
4+
if (callCount === count) {
5+
func();
6+
}
7+
callCount++;
8+
}
9+
}
10+
11+
// /*** Uncomment these to check your work! ***/
12+
const called = function() { console.log('hello') };
13+
const afterCalled = after(3, called);
14+
afterCalled(); // => nothing is printed
15+
afterCalled(); // => nothing is printed
16+
afterCalled(); // => 'hello' is printed

csbin/closure/06-delay.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function delay(func, wait, ...args) {
2+
return function() {
3+
setTimeout(() => func(...args), wait);
4+
}
5+
}

csbin/closure/07-rollcall.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function rollCall(names) {
2+
i = 0;
3+
return function() {
4+
if (i >= names.length) {
5+
console.log('Everyone accounted for');
6+
return;
7+
}
8+
console.log(names[i]);
9+
i++;
10+
}
11+
}
12+
13+
const rollCaller = rollCall(['Victoria', 'Juan', 'Ruth'])
14+
rollCaller() // => should log 'Victoria'
15+
rollCaller() // => should log 'Juan'
16+
rollCaller() // => should log 'Ruth'
17+
rollCaller() // => should log 'Everyone accounted for'

csbin/closure/08-saveOutput.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function saveOutput(func, magicWord) {
2+
const tracker = {};
3+
return function(val) {
4+
if (val === magicWord) return tracker;
5+
tracker[val] = func(val);
6+
return tracker[val];
7+
}
8+
}
9+
10+
const multiplyBy2 = function(num) { return num * 2; };
11+
const multBy2AndLog = saveOutput(multiplyBy2, 'boo');
12+
console.log(multBy2AndLog(2)); // => should log 4
13+
console.log(multBy2AndLog(9)); // => should log 18
14+
console.log(multBy2AndLog('boo')); // => should log { 2: 4, 9: 18 }

csbin/closure/09-cycleIterator.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function cycleIterator(array) {
2+
let i = 0;
3+
return function() {
4+
const curEl = array[i];
5+
if (i === array.length - 1) {
6+
i = 0;
7+
return curEl;
8+
}
9+
i++;
10+
return curEl;
11+
}
12+
}
13+
14+
const threeDayWeekend = ['Fri', 'Sat', 'Sun'];
15+
const getDay = cycleIterator(threeDayWeekend);
16+
console.log(getDay()); // => should log 'Fri'
17+
console.log(getDay()); // => should log 'Sat'
18+
console.log(getDay()); // => should log 'Sun'
19+
console.log(getDay()); // => should log 'Fri'

csbin/closure/10-defineFirstArg.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function defineFirstArg(func, arg) {
2+
return function(...args) {
3+
return func(arg, ...args);
4+
}
5+
}
6+
7+
const subtract = function(big, small) { return big - small; };
8+
const subFrom20 = defineFirstArg(subtract, 20);
9+
console.log(subFrom20(5)); // => should log 15

0 commit comments

Comments
 (0)