Repository: bgando/functionalJS Branch: master Commit: cf2c08090dc5 Files: 11 Total size: 244.0 KB Directory structure: gitextract_d4lvjnne/ ├── README.md ├── callbacks/ │ └── callbackExercises.md ├── closures/ │ └── closureExercises.md ├── scope/ │ ├── SpecRunner.html │ ├── functions.js │ ├── lib/ │ │ ├── chai/ │ │ │ └── chai.js │ │ └── mocha/ │ │ ├── mocha.css │ │ └── mocha.js │ └── scopeExercises.md ├── underscore-extra-credit/ │ └── underscoreExercises.md └── underscore-loops/ └── callbackExercises.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # JSFundamentals Course Description Come here to solidify your understanding of functions and get started with functional programming in JavaScript. This is a highly interactive course that uses exercises and projects to drive home these JS Fundamentals. You will finish this course with deep knowledge on one of the most important aspects of the language: functions. ## Goals of this course The goal of the JavaScript Fundamentals part 1 and 2 is to give technical foundation and support for those who have been learning JavaScript for a couple of months, so that they will have reached a level required for the Hack Reactor admission's interviews. *IMPORTANT*: completion of the HRJR courses is no guarantee of admission, nor has any influence in the admissions process. It is our goal, however, that students who successfully complete the HRJR series will have covered all JavaScript topics necessary for the interview. If Hack Reactor takes people from 60 to 120 in web development, then Hack Reactor Junior takes people from 10 to 60 ('coz we don't teach variables and the like). ## Prerequisites It is recommended that students take JS Fundamentals: Objects, Arrays and Functions prior to this course; however it is not required. Students should have 3-5 months experience with JavaScript and programming. Students should understand and be familiar with variables, if/else statements, loops, objects, arrays and the most basic of JavaScript syntax. ## Textbook No textbook is required for this course. All materials are included in this github repo. ## Technical requirements Laptop, Chrome browser and a text editor. We recommend Sublime Text 3 for this course because it's fast, lightweight and you can run your JavaScript files in its console with Node. ## Resources **Scope and Closure:** http://speakingjs.com/es5/ch16.html ================================================ FILE: callbacks/callbackExercises.md ================================================ # Callback Exercises 1. Write a function, `funcCaller`, that takes a `func` (a function) and an `arg` (any data type). The function returns the `func` called with `arg`(as an argument). 1. Write a function, `firstVal`, that takes an array, `arr`, and a function, `func`, and calls `func` with the first index of the `arr`, the index # and the whole array. 1. Change `firstVal` to work not only with arrays but also objects. Since objects are not ordered, you can use any key-value pair on the object. 1. [Extra Credit] Write a function, `once`, (see: http://underscorejs.org/#once) that takes a function and returns a version of that function which can only be called once. [Hint: you need a closure] You probably don't want to be able to double charge someone's credit card. Here is an example of how to use it: ```javascript var chargeCreditCard = function(num, price){ //charges credit card for a certain price }; var processPaymentOnce = once(chargeCreditCard); processPaymentOnce(123456789012, 200); ``` ================================================ FILE: closures/closureExercises.md ================================================ ## 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. ================================================ FILE: scope/SpecRunner.html ================================================