{
"name": "Object Oriented and Functional Programming",
"order": 6,
"note": [
"Methods",
"Closures",
"Factories",
"Pure Functions",
"Currying Functions",
"Functors",
"Currying Functions"
],
"challenges": [
{
"id":"cf1111c1c15feddfaeb1bdef",
"title": "Declare JavaScript Objects as Variables",
"difficulty":0,
"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":[
"//Here is a sample Object",
"var car = {",
" \"wheels\":4,",
" \"engines\":1,",
" \"seats\":5",
"};",
"",
"//Now Let's make a similar Object called motorBike",
"//Give it two wheels, one engine and one seat",
"var motorBike = {",
" // Only change code below this line.",
"",
"",
"",
" // Only change code above this line.",
"};",
"",
"(function() {return JSON.stringify(motorBike);})();"
],
"challengeType":1,
"type": "waypoint",
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb2bdef",
"title": "Construct JavaScript Objects with Functions",
"difficulty":0,
"description":[
"We are also able to create objects using constructor
functions.",
"Give your myMotorBike
object a wheels
, engines
and seats
attribute 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":[
"// Let's add the properties engines and seats to the car in the same way that the property wheels has been added below. They should both be numbers.",
"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() {",
"",
"",
"",
"};",
"",
"var myMotorBike = new MotorBike();",
"// Only change code above this line.",
"",
"(function() {return JSON.stringify(myMotorBike);})();"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb3bdef",
"title":"Make Object Properties Private",
"difficulty":0,
"description":[
"Objects have their own attributes, called properties
, and their own functions, called methods
.",
"In the previous challenge, we used the this
keyword to reference public properties
and public methods
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 omit the word this
from the property
or method
declaration.",
"See if you can keep myBike.speed
and myBike.addUnit
private, while making myBike.getSpeed
publicly accessible."
],
"tests":[
"assert(typeof(myBike.getSpeed)!=='undefined' && typeof(myBike.getSpeed) === 'function', 'message: The method getSpeed of myBike should be accessible outside the object.');",
"assert(typeof(myBike.speed) === 'undefined', 'message: myBike.speed
should remain undefined.');",
"assert(typeof(myBike.addUnit) === 'undefined', 'message: myBike.addUnit
should remain undefined.');"
],
"challengeSeed":[
"//Let's create an object with a two functions. One attached as a property and one not.",
"var Car = function() {",
" this.gear = 1;",
" function addStyle(styleMe){",
" return 'The Current Gear Is: ' + styleMe;",
" }",
" this.getGear = function() {",
" return addStyle(this.gear);",
" };",
"};",
"",
"var Bike = function() {",
" // Only change code below this line.",
" this.speed = 100;",
" function addUnit(value) {",
" return value + \"KM/H\";",
" }",
" ",
" getSpeed = function () {",
" return addUnit(speed);",
" };",
" ",
"};",
"",
"// Only change code above this line.",
"var myCar = new Car();",
"var myBike = new Bike();",
"",
"if(myBike.hasOwnProperty('getSpeed')){(function() {return JSON.stringify(myBike.getSpeed());})();};"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb4bdef",
"title":"Make Instances of Objects with a Constructor Function",
"difficulty":0,
"description":[
"Sometimes you'll want to be able to easily create similar objects.",
"Objects have their own attributes, called properties
, and their own functions, called methods
.",
"A function that creates objects is called a constructor
.",
"You can create instances
of an object using a constructor
.",
"Each new instance
of this object inherits
all the properties
and methods
of your original object.",
"Then you can give the instance new properties."
],
"tests":[
"assert((new Car()).wheels === 4, 'message: The property wheels
should still be 4 like in the object constructor.');",
"assert(typeof((new Car()).engines) === 'undefined', 'message: There should not be a property engines
in the object constructor.');",
"assert(myCar.wheels === 4, 'message: The property wheels
of myCar should equal 4.');",
"assert(typeof(myCar.engines) === 'number', 'message: The property engines
of myCar should be a number.');"
],
"challengeSeed":[
"var Car = function() {",
" this.wheels = 4;",
"};",
"",
"// Only change code below this line.",
"var myCar = new Car();",
"",
"//Add the property \"engines\" to myCar, and make it a number.",
"",
"",
"// Only change code above this line.",
"(function() {return JSON.stringify(myCar);})();"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb7bdef",
"title":"Iterate over Arrays with .map",
"difficulty":0,
"description":[
"array = array.map(function(val){
",
" return val+1;
",
"});
",
"",
"The map method is one of the easiest ways to iterate through an array or object there is. Let's use it now.",
"Use the map function to add 3 to every value in the variable array
"
],
"tests":[
"assert.deepEqual(array, [4,5,6,7,8], 'message: You should add three to each value in the array.');",
"assert(editor.getValue().match(/\\.map\\(/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":[
"//Use map to add three to each value in the array",
"var array = [1,2,3,4,5];",
"// Only change code below this line.",
"",
"",
"",
"// Only change code above this line.",
"(function() {return array;})();"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb8bdef",
"title":"Condense arrays with .reduce",
"difficulty":0,
"description":[
"Reduce can be useful for condensing an array or numbers into one value.",
"var singleVal = array.reduce(function(previousVal, currentVal){
",
" return previousVal+currentVal;
",
"});
"
],
"tests":[
"assert(singleVal == 30, 'message: singleVal
should have been set to the result of you reduce operation.');",
"assert(editor.getValue().match(/\\.reduce\\(/gi), 'message: You should have made use of the reduce method.');"
],
"challengeSeed":[
"var array = [4,5,6,7,8];",
"var singleVal = 0;",
"// Only change code below this line.",
"",
"",
"",
"// Only change code above this line.",
"(function() {return singleVal;})();"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c15feddfaeb9bdef",
"title":"Filter Arrays with .filter",
"difficulty":0,
"description":[
"filter is a useful method that can filter out values that don't match a certain criteria",
"Let's remove all the values greater than five",
"array = array.filter(function(val) {
",
" return val <= 5;
",
"});
"
],
"tests":[
"assert.deepEqual(array, [1,2,3,4], 'message: You should have removed all the values from the array that are greater than 4.');",
"assert(editor.getValue().match(/array\\.filter\\(/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 array = [1,2,3,4,5,6,7,8,9,10];",
" // Only change code below this line.",
"",
"",
"",
" // Only change code above this line.",
"(function() {return array;})();"
],
"challengeType":1,
"type": "waypoint"
},
{
"id":"cf1111c1c16feddfaeb1bdef",
"title": "Sort Arrays with .sort",
"difficulty":0,
"description":[
"You can use the method sort to easily sort the values in the array alphabetically or numerically",
"var array = [1,3,2];
",
"array = array.sort();
",
"This will return [1, 2, 3]
"
],
"tests":[
"assert.deepEqual(array, ['alpha', 'beta', 'charlie'], 'message: You should have sorted the array alphabetically.');",
"assert(editor.getValue().match(/\\[\\'beta\\'\\,\\s\\'alpha\\'\\,\\s'charlie\\'\\];/gi), 'message: You should be sorting the array using sort.');",
"assert(editor.getValue().match(/\\.sort\\(\\)/gi), 'message: You should have made use of the sort method.');"
],
"challengeSeed":[
"var array = ['beta', 'alpha', 'charlie'];",
"// Only change code below this line.",
"",
"",
"",
" // 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()
function to reverse the contents of an array."
],
"tests": [
"assert.deepEqual(array, [7,6,5,4,3,2,1], 'message: You should reverse the array.');",
"assert(editor.getValue().match(/\\.reverse\\(\\)/gi), 'message: You should use the reverse method.');",
"assert(editor.getValue().match(/\\[1\\,2\\,3\\,4\\,5\\,6\\,7/gi), 'message: You should return [7,6,5,4,3,2,1]
.');"
],
"challengeSeed": [
"var array = [1,2,3,4,5,6,7];",
" // Only change code below this line.",
"",
"",
"",
" // Only change code above this line.",
"(function() {return array;})();"
],
"challengeType": 1,
"type": "waypoint"
},
{
"id": "cf1111c1c16feddfaeb3bdef",
"title": "Concatenate Strings with .concat",
"description": [
".concat()
can be used to merge the contents of two arrays into one.",
"array = array.concat(otherArray);
"
],
"tests": [
"assert.deepEqual(array, [1,2,3,4,5,6], 'You should concat the two arrays together.');",
"assert(editor.getValue().match(/\\.concat\\(/gi), 'message: You should be use 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 modify the two arrays without changing the origional ones.');"
],
"challengeSeed": [
"var array = [1,2,3];",
"",
"var concatMe = [4,5,6];",
"// Only change code below this line.",
"",
"",
"",
"// Only change code above this line.",
"(function() {return array;})();"
],
"challengeType": 1,
"type": "waypoint"
},
{
"id":"cf1111c1c16feddfaeb4bdef",
"title":"Split Strings with .split",
"difficulty":0,
"description":[
"You can use the .split()
method to split a string into an array.",
"split uses the argument you give to to split the string.",
"array = string.split(' ');
"
],
"tests":[
"assert(typeof(array) === 'object' && array.length === 5, 'message: You should split the string by its spaces.');",
"assert(/\\.split\\(/gi, 'message: You should use the split method on the string.');"
],
"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",
"difficulty":0,
"description":[
"We can use the .join()
method to join each element in an array into a string separated by whatever delimiter you provide as an argument to the join operation.",
"var joinMe = joinMe.join(\" \");
"
],
"tests":[
"assert(typeof(joinMe) === 'string' && joinMe === \"Split me into an array\", 'message: You should join the arrays by their 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.",
"",
"joinMe = joinMe;",
"",
"// Only change code above this line.",
"(function() {return joinMe;})();"
],
"challengeType":1,
"type": "waypoint"
}
]
}