Skip to content

Commit 2c68049

Browse files
committed
initial commit
0 parents  commit 2c68049

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

closures.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Closures</title>
5+
</head>
6+
7+
<body>
8+
<h1>Closures</h1>
9+
<script src="closures.js"></script>
10+
</body>
11+
</html>

closures.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Example
2+
const createCounter = () => {
3+
let count = 0;
4+
const getCount = () => count;
5+
const increaseCount = () => count++;
6+
7+
return {
8+
getCount,
9+
increaseCount
10+
};
11+
};
12+
const counter = createCounter();
13+
counter.increaseCount();
14+
console.log(counter.getCount());
15+
// 1
16+
17+
// Exercise:
18+
// This function should create an empty array of messages, and should then
19+
// return an object with the following methods:
20+
// - addMessage method that adds a message to the array
21+
// - getMessage(index) method that returns the message at index index
22+
23+
const createMessageHolder = () => {};
24+
25+
// Test
26+
const messageHolder = createMessageHolder();
27+
messageHolder.addMessage('Hello!');
28+
messageHolder.addMessage('Goodbye!');
29+
console.log(messageHolder.getMessage(0));
30+
// "Hello!""
31+
32+
// Example: non-currying
33+
const addNumbers = function(num1, num2) {
34+
return num1 + num2;
35+
};
36+
console.log(addNumbers(5, 3));
37+
// 8
38+
39+
// Example: currying
40+
const addToNumber = function(num1) {
41+
const addToFirst = function(num2) {
42+
return num1 + num2;
43+
};
44+
return addToFirst;
45+
};
46+
const addThree = addToNumber(3);
47+
console.log(addThree(9));
48+
// 12
49+
50+
console.log(addThree(41));
51+
// 44
52+
53+
// Create a function createGreeting
54+
// - This should accept a single argument: greeting (i.e. "Hello")
55+
// This will return a function a function greet
56+
// - This accepts a single argument, name (i.e. "Matt")
57+
// - This function should return the greeting combined with the name, (i.e. "Hello Matt")
58+
const createGreeting = function(greeting) {};
59+
60+
// Test
61+
const welcomeGreet = createGreeting('Welcome');
62+
console.log(welcomeGreet('Alice'));
63+
64+
const helloGreet = createGreeting('Hello');
65+
console.log(helloGreet('Winnie'));

higher-order.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Higher Order Functions</title>
5+
</head>
6+
7+
<body>
8+
<script src="higher-order.js"></script>
9+
</body>
10+
</html>

higher-order.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This function should execute the callback function the number of times specified.
2+
// When the function is being executed, the repetition number (i.e. 1 for the first call)
3+
// should be passed to the callback.
4+
const repeatFn = (times, callback) => {};
5+
6+
// Test repeatFn
7+
const addButton = num => {
8+
const button = document.createElement('button');
9+
button.innerText = `Button ${num}`;
10+
document.querySelector('body').appendChild(button);
11+
};
12+
repeatFn(6, addButton);
13+
14+
const toThePower = (num, pow) => {
15+
let product = 1;
16+
repeatFn(pow, () => {
17+
product += product * num;
18+
});
19+
return product;
20+
};
21+
22+
console.log(toThePower(3, 3));

0 commit comments

Comments
 (0)