-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #1
base: master
Are you sure you want to change the base?
Develop #1
Changes from all commits
700d995
c0d6f1e
f88bd0d
10e2e9c
d91f1d0
7efc755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 1. | ||
function nonsense(string) { | ||
var blab = function(){ | ||
alert(string); | ||
}; | ||
setTimeout(blab,2000); | ||
|
||
return blab; | ||
} | ||
// 2. | ||
var blabLater = nonsense('test string1'); | ||
|
||
var blabAgainLater = nonsense('test string2'); | ||
|
||
// 3. | ||
var lastName = function (lName) { | ||
return function firstName(fName) { | ||
console.log(lName + " " + fName); | ||
}; | ||
}; | ||
var newLast = lastName('Regu'); | ||
newLast('Aman'); | ||
var firstNameFarmer = lastName('Farmer'); | ||
firstNameFarmer('Brown'); | ||
var farmLoveStory = storyWriter(); | ||
farmLoveStory.addWords('There was once a lonely cow.'); // 'There was once a lonely cow.' | ||
farmLoveStory.addWords('It saw a friendly face.'); //'There was once a lonely cow. It saw a friendly face.' | ||
|
||
var storyOfMyLife = storyWriter(); | ||
storyOfMyLife.addWords('My code broke.'); // 'My code broke.' | ||
storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.' | ||
storyOfMyLife.erase(); // '' | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
## Closure exercises | ||
|
||
1. Write a function, `nonsense` that takes an input `string`. This function contains another function, `blab` which alerts `string` and is immediately called inside the function `nonsense`. `blab` should look like this inside of the `nonsense` function: | ||
|
||
```javascript | ||
var blab = function(){ | ||
alert(string); | ||
}; | ||
``` | ||
|
||
1. In your function, `nonsense`, change the immediate call to a setTimeout so that the call to `blab` comes after 2 seconds. The `blab` function itself should stay the same as before. | ||
|
||
1. Now, instead of calling `blab` inside of `nonsense`, return `blab` (without invoking it). Call `nonsense` with some string and store the returned value (the `blab` function) in a variable called `blabLater`. Call `nonsense` again with a different string and store the returned value in a variable called `blabAgainLater`. | ||
|
||
1. Inspect `blabLater` and `blabAgainLater` in your console. Call them (they are functions!) and see what happens! | ||
|
||
|
||
1. Write a function with a closure. The first function should only take one argument, someone's first name, and the inner function should take one more argument, someone's last name. The inner function should console.log both the first name and the last name. | ||
```javascript | ||
var lastNameTrier = function(firstName){ | ||
//does stuff | ||
|
||
var innerFunction = function() { | ||
//does stuff | ||
}; | ||
//maybe returns something here | ||
}; | ||
var firstNameFarmer = lastNameTrier('Farmer'); //logs nothing | ||
firstNameFarmer('Brown'); //logs 'Farmer Brown' | ||
``` | ||
This function is useful in case you want to try on different last names. For example, I could use firstName again with another last name: | ||
|
||
```javascript | ||
firstNameFarmer('Jane'); //logs 'Farmer Jane' | ||
firstNameFarmer('Lynne'); //logs 'Farmer Lynne' | ||
``` | ||
|
||
|
||
1. Create a `storyWriter` function that returns an object with two methods. One method, `addWords` adds a word to your story and returns the story while the other one, `erase`, resets the story back to an empty string. Here is an implementation: | ||
```javascript | ||
var farmLoveStory = storyWriter(); | ||
farmLoveStory.addWords('There was once a lonely cow.'); // 'There was once a lonely cow.' | ||
farmLoveStory.addWords('It saw a friendly face.'); //'There was once a lonely cow. It saw a friendly face.' | ||
|
||
var storyOfMyLife = storyWriter(); | ||
storyOfMyLife.addWords('My code broke.'); // 'My code broke.' | ||
storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.' | ||
storyOfMyLife.erase(); // '' | ||
|
||
``` | ||
|
||
1. Using the module pattern, design a toaster. Use your creativity here and think about what you want your users to be able to access on the outside of your toaster vs what you don't want them to be able to touch. | ||
|
||
```javascript | ||
var Toaster = function(){ | ||
//some private methods and properties | ||
|
||
return { | ||
//some public methods and properties, etc | ||
}; | ||
}; | ||
``` | ||
|
||
|
||
1. [EXTRA CREDIT] Use the module pattern to design a character in a Super Mario game. Think about what actions you can control in the game and other aspects you can't control directly (example: you can only affect your health indirectly by eating a mushroom). If you are not familiar with Super Mario, choose another simple game for this example. | ||
|
||
1. [EXTRA CREDIT] Why doesn't the code below work? This is a function that should return an array of functions that console.log() each person's name as a string when invoked. Fiddle with this function and inspect how it works, then try to fix it using a closure. Be prepared to explain to a partner how it worked before, and how it works now with a closure. | ||
|
||
```javascript | ||
var checkAttendanceFunc = function(nameArr){ | ||
var resultArr = []; | ||
for(var i = 0; i < nameArr.length; i++){ | ||
resultArr.push(function(){ console.log('Is', nameArr[i], 'present?', i)}) | ||
}; | ||
return resultArr; | ||
}; | ||
``` | ||
Here is a hint: http://jsfiddle.net/PuEy6/ | ||
|
||
1. [EXTRA CREDIT] Write a function that takes another function\* as an argument and creates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call. How could you do this without using a closure? Is it even possible? How could you do this with a closure? \*Note: This original input function should *not* have any parameters. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Create the execution context flow of the this function | ||
|
||
var counter = (function() { | ||
var privateCounter = 0; | ||
function changeBy(val) { | ||
privateCounter += val; | ||
} | ||
return { | ||
increment: function() { | ||
changeBy(1); | ||
}, | ||
decrement: function() { | ||
changeBy(-1); | ||
}, | ||
value: function() { | ||
return privateCounter; | ||
} | ||
}; | ||
})(); | ||
|
||
counter.value(); | ||
counter.increment(); | ||
counter.increment(); | ||
counter.value(); | ||
counter.decrement(); | ||
counter.value(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
// Challenge 1. Flattening | ||
// Challenge 1. Flattening | ||
let arrays = [[1, 2, 3], [4, 5], [6]]; | ||
function flatten(arrays) { | ||
return arrays.reduce((accumelator,currentValue) => { | ||
return accumelator.concat(currentValue) | ||
}) | ||
} | ||
|
||
// Your code here. | ||
// → [1, 2, 3, 4, 5, 6] | ||
|
||
// Challenge 2. Your own loop | ||
// Your code here. | ||
function loop(n,condition,update,body) { | ||
for(let n = n; condition; update) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cant have a function reference as the |
||
var check = condition | ||
if ( check(n) === true) { | ||
var upda = update; | ||
var num = update(n); | ||
body(num) | ||
} else return; | ||
} | ||
|
||
loop(3, n => n > 0, n => n - 1, console.log); | ||
// → 3 | ||
|
@@ -14,7 +28,10 @@ loop(3, n => n > 0, n => n - 1, console.log); | |
|
||
// Challenge 3. Everything | ||
function every(array, test) { | ||
// Your code here. | ||
for(let i of array) { | ||
if(!test(i)) return false | ||
} | ||
return true; | ||
} | ||
|
||
console.log(every([1, 3, 5], n => n < 10)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
// Challenge 1 | ||
function addTwo(num) {} | ||
function addTwo(num) { | ||
return num + 2 | ||
} | ||
|
||
// To check if you've completed it, uncomment these console.logs! | ||
// console.log(addTwo(3)); | ||
// console.log(addTwo(10)); | ||
|
||
// Challenge 2 | ||
function addS(word) {} | ||
function addS(word) { | ||
return word.concat("s") | ||
} | ||
|
||
// uncomment these to check your work | ||
// console.log(addS('pizza')); | ||
// console.log(addS('bagel')); | ||
|
||
// Challenge 3 | ||
function map(array, callback) {} | ||
function multiplyByTwo(n) { | ||
return n*2; | ||
} | ||
function map(array,callback) { | ||
var newArray = []; | ||
for(let i = 0; i < array.length; i++) { | ||
newArray.push(callback(array[i])); | ||
} | ||
return newArray; | ||
} | ||
|
||
// console.log(map([1, 2, 3], addTwo)); | ||
|
||
// Challenge 4 | ||
function forEach(array, callback) {} | ||
function forEach(array, callback) { | ||
for (let i = 0; i < array.length; i++) { | ||
callback(array[i]) | ||
} | ||
} | ||
|
||
// see for yourself if your forEach works! | ||
|
||
|
@@ -27,31 +44,92 @@ function forEach(array, callback) {} | |
//-------------------------------------------------- | ||
|
||
//Extension 1 | ||
function mapWith(array, callback) {} | ||
function multiplyByTwo(n) { | ||
return n*2; | ||
} | ||
function forEach(array, callback) { | ||
for (let i = 0; i < array.length; i++) { | ||
callback(array[i]) | ||
} | ||
} | ||
function mapWith(array,callback) { | ||
var newArray = []; | ||
forEach(array, (num)=> { | ||
newArray.push(callback(num)) | ||
}) | ||
return newArray; | ||
}; | ||
|
||
//Extension 2 | ||
function reduce(array, callback, initialValue) {} | ||
function reduce(array, callback, initialValue) { | ||
var accumelator = initialValue; | ||
for(let i = 0; i < array.length; i++) { | ||
accumelator += callback(accumelator,array[i]); | ||
} return accumelator | ||
} | ||
|
||
//Extension 3 | ||
function intersection(arrays) {} | ||
function intersection(...arrays) { | ||
let result = []; | ||
arrays.reduce((acc, cv) => { | ||
acc.forEach(element => { | ||
if (cv.includes(element) && !result.includes(element)) { | ||
result.push(element);} | ||
}); | ||
return result; | ||
}); | ||
return result; | ||
} | ||
|
||
// console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20])); | ||
// should log: [5, 15] | ||
|
||
//Extension 4 | ||
function union(arrays) {} | ||
function union(...arrays) { | ||
let result = []; | ||
arrays.reduce((acc, cv) => { | ||
cv.forEach(item => { | ||
if (!acc.includes(item)) { | ||
result.push(item); | ||
} | ||
}); | ||
return result; | ||
}, []); | ||
return result; | ||
} | ||
|
||
// console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5])); | ||
// should log: [5, 10, 15, 88, 1, 7, 100] | ||
|
||
//Extension 5 | ||
function objOfMatches(array1, array2, callback) {} | ||
function objOfMatches(array1, array2, callback) { | ||
let obj = {}; | ||
array1.reduce((accumulator, currentValue) => { | ||
let output = callback(currentValue); | ||
let index = array1.indexOf(currentValue); | ||
if (output == array2[index]) { | ||
accumulator[currentValue] = array2[index]; | ||
} | ||
obj = accumulator; | ||
return accumulator; | ||
}, {}); | ||
return obj; | ||
} | ||
|
||
// console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); })); | ||
// should log: { hi: 'HI', bye: 'BYE', later: 'LATER' } | ||
|
||
//Extension 6 | ||
function multiMap(arrVals, arrCallbacks) {} | ||
function multiMap(arrVals, arrCallbacks) { | ||
let result = {}; | ||
arrVals.reduce((accumulator, currentValue) => { | ||
accumulator[currentValue] = arrCallbacks.map(item => item(currentValue)); | ||
result = accumulator; | ||
return accumulator; | ||
}, {}); | ||
return result; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to save the result in function multiMap(arrVals, arrCallbacks) {
return arrVals.reduce((accumulator, currentValue) => {
accumulator[currentValue] = arrCallbacks.map(item => item(currentValue));
return accumulator;
}, {});
} |
||
|
||
// console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }])); | ||
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not complete yet.