{ "name": "Object Oriented and Functional Programming", "order": 7, "time": "2h", "note": [ "Methods", "Closures", "Factories", "Pure Functions", "Currying Functions", "Functors", "Currying Functions" ], "challenges": [ { "id":"cf1111c1c15feddfaeb1bdef", "title": "Declare JavaScript Objects as Variables", "description":[ "Before we dive into Object Oriented Programming, let's revisit JavaScript objects.", "Give your motorBike object a wheels, engines and seats attribute and set them to numbers." ], "tests":[ "assert(typeof motorBike.engines === 'number', 'message: motorBike should have a engines attribute set to a number.');", "assert(typeof motorBike.wheels === 'number', 'message: motorBike should have a wheels attribute set to a number.');", "assert(typeof motorBike.seats === 'number', 'message: motorBike should have a seats attribute set to a number.');" ], "challengeSeed":[ "var car = {", " \"wheels\":4,", " \"engines\":1,", " \"seats\":5", "};", "", "var motorBike = {", "", " // Only change code below this line.", "", "", "", " // Only change code above this line.", "", "};" ], "tail":[ "(function() {return JSON.stringify(motorBike);})();" ], "solutions":[ "var car = {\n \"wheels\":4,\n \"engines\":1,\n \"seats\":5\n};\n\nvar motorBike = {\n \"wheels\": 4,\n \"engines\": 1,\n \"seats\": 2\n};" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb2bdef", "title": "Construct JavaScript Objects with Functions", "description":[ "We are also able to create objects using constructor functions.", "A constructor function is given a capitalized name to make it clear that it is a constructor.", "Here's an example of a constructor function:", "var Car = function() {", "  this.wheels = 4;", "  this.engines = 1;", "  this.seats = 1;", "};", "In a constructor the this variable refers to the new object being created by the constructor. So when we write,", "  this.wheels = 4;", "inside of the constructor we are giving the new object it creates a property called wheels with a value of 4.", "You can think of a constructor as a description for the object it will create.", "Have your MotorBike constructor describe an object with wheels, engines and seats properties and set them to numbers." ], "tests":[ "assert(typeof (new MotorBike()).engines === 'number', 'message: myMotorBike should have a engines attribute set to a number.');", "assert(typeof (new MotorBike()).wheels === 'number', 'message: myMotorBike should have a wheels attribute set to a number.');", "assert(typeof (new MotorBike()).seats === 'number', 'message: myMotorBike should have a seats attribute set to a number.');" ], "challengeSeed":[ "var Car = function() {", " this.wheels = 4;", " this.engines = 1;", " this.seats = 1;", "};", "", "var myCar = new Car();", "", "// Only change code below this line.", "", "var MotorBike = function() {", "", "", "", "};" ], "tail":[ "var myMotorBike = new MotorBike();", "(function() {return JSON.stringify(myMotorBike);})();" ], "solutions":[ "var Car = function() {\n this.wheels = 4;\n this.engines = 1;\n this.seats = 1;\n};\n\nvar myCar = new Car();\n\nvar MotorBike = function() {\n this.engines = 1;\n this.seats = 1;\n this.wheels = 4;\n};\n\nvar myMotorBike = new MotorBike();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb4bdef", "title":"Make Instances of Objects with a Constructor Function", "description":[ "Now let's put that great constructor function we made in the last lesson to use!", "To use a constructor function we call it with the new keyword in front of it like:", "var myCar = new Car();", "myCar is now an instance of the Car constructor that looks like the object it described:", "{", "  wheels: 4,", "  engines: 1,", "  seats: 1", "}", "Note that it is important to use the new keyword when calling a constructor. This is how Javascript knows to create a new object and that all the references to this inside the constructor should be referring to this new object.", "Now, once the myCar instance is created it can be used like any other object and can have its properties accessed and modified the same way you would usually. For example:", "myCar.turboType = \"twin\";", "Our myCar variable now has a property turboType with a value of \"twin\".", "In the editor, use the Car constructor to create a new instance and assign it to myCar.", "Then give myCar a nickname property with a string value." ], "tests":[ "assert((new Car()).wheels === 4, 'message: The property wheels should still be 4 in the object constructor.');", "assert(typeof (new Car()).nickname === 'undefined', 'message: There should not be a property nickname in the object constructor.');", "assert(myCar.wheels === 4, 'message: The property wheels of myCar should equal 4.');", "assert(typeof myCar.nickname === 'string', 'message: The property nickname of myCar should be a string.');" ], "challengeSeed":[ "var Car = function() {", " this.wheels = 4;", " this.engines = 1;", " this.seats = 1;", "};", "", "// Only change code below this line.", "", "var myCar;" ], "tail":[ "(function() {return JSON.stringify(myCar);})();" ], "solutions":[ "var Car = function() {\n this.wheels = 4;\n this.engines = 1;\n this.seats = 1;\n};\n\nvar myCar = new Car();\n\nmyCar.nickname = \"Lucy\";" ], "challengeType":1, "type": "waypoint" }, { "id":"563cfb55594311ffcb333c70", "title":"Make Unique Objects by Passing Parameters to our Constructor", "description":[ "The constructor we have is great, but what if we don't always want to create the same object?", "To solve this we can add parameters to our constructor. We do this like the following example:", "var Car = function(wheels, seats, engines) {", "  this.wheels = wheels;", "  this.seats = seats;", "  this.engines = engines;", "};", "Now we can pass in arguments when we call our constructor.", "var myCar = new Car(6, 3, 1);", "This code will create an object that uses the arguments we passed in and looks like:", "{", "  wheels: 6,", "  seats: 3,", "  engines: 1", "}", "Now give it a try yourself! Alter the Car constructor to use parameters to assign values to the wheels, seats, and engines properties.", "Then call your new constructor with three number arguments and assign it to myCar to see it in action." ], "tests":[ "assert((function(){var testCar = new Car(3,1,2); return testCar.wheels === 3 && testCar.seats === 1 && testCar.engines === 2;})(), 'message: Calling new Car(3,1,2) should produce an object with a wheels property of 3, a seats property of 1, and an engines property of 2.');", "assert(typeof myCar.wheels === 'number' && typeof myCar.seats === 'number' && typeof myCar.engines === 'number', 'message: myCar should have number values for the wheels, seats, and engines properties.');" ], "challengeSeed":[ "var Car = function() {", " //Change this constructor", " this.wheels = 4;", " this.seats = 1;", " this.engines = 1;", "};", "", "//Try it out here", "var myCar;", "", "// Only change code above this line", "", "(function() {return JSON.stringify(myCar);})();" ], "solutions":[ "var Car = function(wheels,seats,engines) {\n this.wheels = wheels;\n this.seats = seats;\n this.engines = engines;\n};\n\nvar myCar = new Car(4,1,1);" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb3bdef", "title":"Make Object Properties Private", "description":[ "Objects have their own attributes, called properties, and their own functions, called methods.", "In the previous challenges, we used the this keyword to reference public properties of the current object.", "We can also create private properties and private methods, which aren't accessible from outside the object.", "To do this, we create the variable inside the constructor using the var keyword we're familiar with, instead of creating it as a property of this.", "This is useful for when we need to store information about an object but we want to control how it is used by outside code.", "For example, what if we want to store the speed our car is traveling at but we only want outside code to be able to modify it by accelerating or decelerating, so the speed changes in a controlled way?", "In the editor you can see an example of a Car constructor that implements this pattern.", "Now try it yourself! Modify the Bike constructor to have a private property called gear and two public methods called getGear and setGear to get and set that value." ], "tests":[ "assert(typeof myBike.getGear !== 'undefined' && typeof(myBike.getGear) === 'function', 'message: The method getGear of myBike should be accessible outside the object.');", "assert(typeof myBike.setGear !== 'undefined' && typeof(myBike.setGear) === 'function', 'message: The method setGear of myBike should be accessible outside the object.');", "assert(typeof myBike.gear === 'undefined', 'message: myBike.gear should remain undefined.');" ], "challengeSeed":[ "var Car = function() {", " // this is a private variable", " var speed = 10;", "", " // these are public methods", " this.accelerate = function(change) {", " speed += change;", " };", "", " this.decelerate = function() {", " speed -= 5;", " };", "", " this.getSpeed = function() {", " return speed;", " };", "};", "", "var Bike = function() {", "", " // Only change code below this line.", "", "", "", " // Only change code above this line.", "};", "", "var myCar = new Car();", "", "var myBike = new Bike();" ], "tail":[ "if(myBike.hasOwnProperty('getGear')){(function() {return JSON.stringify(myBike.getGear());})();}" ], "solutions":[ "var Car = function() {\n var speed = 10;\n\n this.accelerate = function(change) {\n speed += change;\n };\n\n this.decelerate = function() {\n speed -= 5;\n };\n\n this.getSpeed = function() {\n return speed;\n };\n};\n\nvar Bike = function() {\n var gear = 1;\n \n this.getGear = function() {\n return gear;\n };\n \n this.setGear = function(newGear) {\n gear = newGear;\n };\n};\n\nvar myCar = new Car();\n\nvar myBike = new Bike();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb7bdef", "title":"Iterate over Arrays with .map", "description":[ "The map method is a convenient way to iterate through arrays. Here's an example usage:", "var timesFour = oldArray.map(function(val){", "  return val * 4;", "});", "", "The map method will iterate through every element of the array, creating a new array with values that have been modified by the callback function, and return it.", "In our example the callback only uses the value of the array element (the val argument) but your callback can also include arguments for the index and array being acted on.", "Use the map function to add 3 to every value in the variable oldArray." ], "tests":[ "assert.deepEqual(newArray, [4,5,6,7,8], 'message: You should add three to each value in the array.');", "assert(editor.getValue().match(/\\.map\\s*\\(/gi), 'message: You should be making use of the map method.');", "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\]/gi), 'message: You should only modify the array with map.');" ], "challengeSeed":[ "var oldArray = [1,2,3,4,5];", "", "// Only change code below this line.", "", "var newArray = oldArray;", "", "// Only change code above this line.", "", "(function() {return newArray;})();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb8bdef", "title":"Condense arrays with .reduce", "description":[ "The array method reduce is used to iterate through an array and condense it into one value.", "To use reduce you pass in a callback whose arguments are an accumulator (in this case, previousVal) and the current value (currentVal).", "reduce has an optional second argument which can be used to set the initial value of the accumulator. If no initial value is specified it will be the first array element and currentVal will start with the second array element.", "Here is an example of reduce being used to subtract all the values of an array:", "var singleVal = array.reduce(function(previousVal, currentVal) {", "  return previousVal - currentVal;", "}, 0);", "Use the reduce method to sum all the values in array and assign it to singleVal." ], "tests":[ "assert(singleVal == 30, 'message: singleVal should be equal to the sum of all items in the array variable.');", "assert(editor.getValue().match(/\\.reduce\\s*\\(/gi), 'message: You should have made use of the reduce method.');" ], "challengeSeed":[ "var array = [4,5,6,7,8];", "", "// Only change code below this line.", "", "var singleVal = array;", "", "// Only change code above this line.", "", "(function() {return singleVal;})();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c15feddfaeb9bdef", "title":"Filter Arrays with .filter", "description":[ "The filter method is used to iterate through an array and filter out elements where a given condition is not true.", "filter is passed a callback function which takes the current value (we've called that val) as an argument.", "Any array element for which the callback returns true will be kept and elements that return false will be filtered out.", "The following code is an example of using filter to remove array elements that are equal to five:", "Note: We omit the second and third arguments since we only need the value", "array = array.filter(function(val) {", "  return val !== 5;", "});", "Use filter to remove all elements from array that are greater than 5." ], "tests":[ "assert.deepEqual(newArray, [1,2,3,4,5], 'message: You should have removed all the values from the array that are greater than 5.');", "assert(editor.getValue().match(/array\\.filter\\s*\\(/gi), 'message: You should be using the filter method to remove the values from the array.');", "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\,6\\,7\\,8\\,9\\,10\\]/gi), 'message: You should only be using filter to modify the contents of the array.');" ], "challengeSeed":[ "var oldArray = [1,2,3,4,5,6,7,8,9,10];", "", "// Only change code below this line.", "", "var newArray = oldArray;", "", "// Only change code above this line.", "", "(function() { return newArray; })();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c16feddfaeb1bdef", "title": "Sort Arrays with .sort", "description":[ "You can use the method sort to easily sort the values in an array alphabetically or numerically.", "Unlike the previous array methods we have been looking at, sort actually alters the array in place. However, it also returns this sorted array.", "sort can be passed a compare function as a callback. If no compare function is passed in it will convert the values to strings and sort alphabetically.", "Here is an example of using sort with a compare function that will sort the elements from smallest to largest number:", "var array = [1, 12, 21, 2];", "array.sort(function(a, b) {", "  return a - b;", "});", "Use sort to sort array from largest to smallest." ], "tests":[ "assert.deepEqual(array, [21, 12, 2, 1], 'message: You should have sorted the array from largest to smallest.');", "assert(editor.getValue().match(/\\[1,\\s*12,\\s*21,\\s*2\\];/gi), 'message: You should only be using sort to modify the array.');", "assert(editor.getValue().match(/\\.sort\\s*\\(/g), 'message: You should have made use of the sort method.');" ], "challengeSeed":[ "var array = [1, 12, 21, 2];", "", "// Only change code below this line.", "", "array.sort();", "", "// Only change code above this line.", "", "(function() { return array; })();" ], "challengeType":1, "type": "waypoint" }, { "id": "cf1111c1c16feddfaeb2bdef", "title": "Reverse Arrays with .reverse", "description": [ "You can use the reverse method to reverse the elements of an array.", "reverse is another array method that alters the array in place, but it also returns the reversed array.", "Use reverse to reverse the array variable and assign it to newArray." ], "tests": [ "assert.deepEqual(newArray, [7,6,5,4,3,2,1], 'message: You should reverse the array.');", "assert(editor.getValue().match(/\\.reverse\\s*\\(\\)/gi), 'message: You should use the reverse method.');", "assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\,6\\,7/gi), 'message: You should only be using reverse to modify array.');" ], "challengeSeed": [ "var array = [1,2,3,4,5,6,7];", "", "// Only change code below this line.", "", "var newArray = array;", "", "// Only change code above this line.", "" ], "tail":[ "(function() {return newArray;})();" ], "challengeType": 1, "type": "waypoint" }, { "id": "cf1111c1c16feddfaeb3bdef", "title": "Concatenate Arrays with .concat", "description": [ "concat can be used to merge the contents of two arrays into one.", "concat takes an array as an argument and returns a new array with the elements of this array concatenated onto the end.", "Here is an example of concat being used to concatenate otherArray onto the end of oldArray:", "newArray = oldArray.concat(otherArray);", "Use .concat() to concatenate concatMe onto the end of oldArray and assign it to newArray." ], "tests": [ "assert.deepEqual(newArray, [1,2,3,4,5,6], 'message: You should concatenate the two arrays together.');", "assert(editor.getValue().match(/\\.concat\\s*\\(/gi), 'message: You should be using the concat method to merge the two arrays.');", "assert(editor.getValue().match(/\\[1\\,2\\,3\\]/gi) && editor.getValue().match(/\\[4\\,5\\,6\\]/gi), 'message: You should only be using concat to modify the arrays.');" ], "challengeSeed": [ "var oldArray = [1,2,3];", "", "var concatMe = [4,5,6];", "", "// Only change code below this line.", "", "var newArray = oldArray;", "", "// Only change code above this line.", "", "(function() { return newArray; })();" ], "challengeType": 1, "type": "waypoint" }, { "id":"cf1111c1c16feddfaeb4bdef", "title":"Split Strings with .split", "description":[ "You can use the split method to split a string into an array.", "split uses the argument you pass in as a delimiter to determine which points the string should be split at.", "Here is an example of split being used to split an array at every s character:", "var array = string.split('s');", "Use split to create an array of words from string and assign it to array." ], "tests":[ "assert(/\\.split\\(/gi, 'message: You should use the split method on the string.');", "assert(typeof(array) === 'object' && array.length === 5, 'message: You should split the string by its spaces.');" ], "challengeSeed":[ "var string = \"Split me into an array\";", "", "// Only change code below this line.", "", "var array = string;", "", "// Only change code above this line.", "", "(function() {return array;})();" ], "challengeType":1, "type": "waypoint" }, { "id":"cf1111c1c16feddfaeb5bdef", "title":"Join Strings with .join", "description":[ "We can use the join method to join each element of an array into a string separated by whatever delimiter you provide as an argument.", "The following is an example of using join to join all of the elements of an array into a string with all the elements separated by word `Na`:", "var joinMe = [\"Na \", \"Na \", \"Na \", \"Na \", \"Batman!\"];", "var joinedString = joinMe.join(\"Na \");", "console.log(joinedString);", "Use the join method to create a string from joinMe with spaces in between each element and assign it to joinedString." ], "tests":[ "assert(typeof(joinedString) === 'string' && joinedString === \"Split me into an array\", 'message: You should join the elements of the array with spaces.');", "assert(/\\.join\\(/gi, 'message: You should use of the join method on the array.');" ], "challengeSeed":[ "var joinMe = [\"Split\",\"me\",\"into\",\"an\",\"array\"];", "", "// Only change code below this line.", "", "var joinedString = joinMe;", "", "// Only change code above this line.", "", "(function() {return joinedString;})();" ], "challengeType":1, "type": "waypoint" } ] }