Files
freeCodeCamp/challenges/object-oriented-and-functional-programming.json

503 lines
26 KiB
JSON
Raw Normal View History

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