Skip to content

Commit 189a67f

Browse files
committed
Actually complete, bad feeling about counter
1 parent a460527 commit 189a67f

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ This task focuses on getting practice with callback functions by giving you an a
1919

2020
### Task 3: Array Methods
2121
Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event.
22-
* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
23-
* [ ] Complete the problems provided to you
24-
* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
22+
* [*] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
23+
* [*] Complete the problems provided to you
24+
* [*] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
2525

2626
### Task 4: Closures
2727
We have learned that closures allow us to access values in scope that have already been invoked.

assignments/array-methods.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ console.log('\n\n========== Challenge 5 ==========');
9797

9898
// Problem 1
9999
// What size shirts do we need?
100+
console.log('\n=== Problem 1 ===\n');
100101
const shirtSizes = [];
101102
runners.forEach(runner => shirtSizes.push(runner.shirt_size));
102103
console.log(shirtSizes.sort());
103104

104105
// Problem 2
105106
// Who donated more than $200 and what companies do they represent?
107+
console.log('\n=== Problem 2 ===\n');
106108
const bigDonors = runners.filter(runner => runner.donation > 200);
107109

108110
bigDonors.forEach(donor => console.log(`${donor.first_name} ${donor.last_name}, ${donor.company_name}`));
109111

110112
// Problem 3
111113
// How many of each size shirt do we need?
114+
console.log('\n=== Problem 3 ===\n');
112115

113116
const allShirts = [
114117
{"size":"XS", "quantSize" : 0},

assignments/callbacks.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
console.log('------------ CALLBACKS.JS ------------\n\n');
2-
1+
console.log('\n\n------------------------------------------\n');
2+
console.log('------------ CALLBACKS.JS ------------\n');
3+
console.log('------------------------------------------\n');
34
// Create a callback function and invoke the function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
45

56
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
@@ -85,11 +86,25 @@ console.log('\n\n========== Challenge 5 ?????????????');
8586
function contains(item, list, cb) {
8687
// contains checks if an item is present inside of the given array/list.
8788
// Pass true to the callback if it is, otherwise pass false.
88-
89+
let tOrF = false;
90+
for (let i = 0; i < list.length; i++) {
91+
if (list[i] === item) {
92+
tOrF = true;
93+
}
94+
}
95+
return cb(tOrF);
8996
}
9097

98+
function doesContain(truf) {
99+
if (truf) {
100+
console.log("We have it!");
101+
} else {
102+
console.log("We don't have it");
103+
}
104+
return;
105+
}
91106

92-
107+
contains('Pencil', items, doesContain);
93108

94109
/* STRETCH PROBLEM */
95110
// ========== Stretch Challenge
@@ -100,3 +115,5 @@ function removeDuplicates(array, cb) {
100115
// Pass the duplicate free array to the callback function.
101116
// Do not mutate the original array.
102117
}
118+
119+
console.log('\n\nEnd of callbacks.js');

assignments/closure.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
function ruben() {
4+
let bread = 'Rye';
5+
6+
function layer1() {
7+
let spread = 'Thousand Island';
8+
9+
function layer2() {
10+
let veg = 'Sauerkraut';
11+
let meat = 'Corned Beef';
12+
let cheese = 'Swiss';
13+
14+
console.log('A ruben sandwich is: ' + meat + ' and ' + cheese + ' with ' + veg + ', on ' + bread + ' with ' + spread + ' and then grilled.')
15+
}
16+
layer2();
17+
}
18+
layer1();
19+
}
320

21+
ruben();
422

523
// ==== Challenge 2: Create a counter function ====
24+
25+
let count = 0;
626
const counter = () => {
727
// Return a function that when invoked increments and returns a counter variable.
28+
return function() {
29+
return ++count;
30+
};
831
};
932
// Example usage: const newCounter = counter();
10-
// newCounter(); // 1
11-
// newCounter(); // 2
33+
const newCounter = counter();
34+
console.log(newCounter());
35+
console.log(newCounter());
36+
1237

1338
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1439

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<h1>Javascript II</h1>
1212

1313
<h2>Callbacks</h2>
14-
<script src="assignments/callbacks.js"></script>
14+
<!-- <script src="assignments/callbacks.js"></script> -->
1515

1616
<h2>Array Methods</h2>
17-
<script src="assignments/array-methods.js"></script>
17+
<!-- <script src="assignments/array-methods.js"></script> -->
1818

1919
<h2>Closures</h2>
2020
<script src="assignments/closure.js"></script>

0 commit comments

Comments
 (0)