Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ We have learned that closures allow us to access values in scope that have alrea

## Stretch Goals

* [ ] Go back through the stretch problems that you skipped over and complete as many as you can.
* [x] Go back through the stretch problems that you skipped over and complete as many as you can.
* [ ] Look up what an IIFE is in JavaScript and experiment with them
15 changes: 11 additions & 4 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Create a higher order function and invoke the callback 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.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
const items = ['Pencil', 'Notebook', 'Pencil', 'yo-yo', 'Gum'];

/*

Expand Down Expand Up @@ -68,9 +68,16 @@ contains("elephant", items, function(check) {


/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
function removeDuplicates(toys, foobar) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
}
//let duplicateFree = [];
return foobar(toys);
}
let duplicateFree = function(toys) {

return toys.filter((v, i) => toys.indexOf(v) === i);
//console.log(toys)
};
console.log(removeDuplicates(items, duplicateFree));
29 changes: 25 additions & 4 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,39 @@ let num1 = 0;
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
num1++;
num2 = num1
console.log(num2);

function counter2() {
num2 = num1;
console.log(num2);
}
counter2();
};
counter();
counter();
counter();
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
var number1 = 0;
// Return an object that has two methods called `increment` and `decrement`.
function increment() {
return number1++;
console.log(number1);
}

function decrement() {
number1--;
console.log(number1);
}
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.

};
increment();
increment();
increment();
decrement();
decrement();
};
counterFactory();