From c774749967ad30dd28718efd1469867972cfcddb Mon Sep 17 00:00:00 2001 From: SaintPeter Date: Sat, 26 Dec 2015 23:30:01 -0800 Subject: [PATCH] Fix brs, other fixes --- .../basic-javascript.json | 249 ++++++++++-------- 1 file changed, 136 insertions(+), 113 deletions(-) diff --git a/challenges/01-front-end-development-certification/basic-javascript.json b/challenges/01-front-end-development-certification/basic-javascript.json index a6d4405b1c..f22792c3a7 100644 --- a/challenges/01-front-end-development-certification/basic-javascript.json +++ b/challenges/01-front-end-development-certification/basic-javascript.json @@ -222,7 +222,8 @@ "MYVAR is not the same as MyVar nor myvar. It is possible to have multiple distinct variables with the same name but different casing. It is strongly reccomended that for sake of clarity you do not use this language feature.", "

Best Practice

Write variable names in Javascript in camelCase. In camelCase, variable names made of multiple words have the first word in all lowercase and the first letter of each subsequent word(s) capitalized.
", " ", - "Examples:
var someVariable;
var anotherVariableName;
var thisVariableNameIsTooLong;
", + "Examples:", + "
var someVariable;
var anotherVariableName;
var thisVariableNameIsTooLong;
", "

Instructions

", "Correct the variable assignements so their names match their variable declarations above." ], @@ -270,7 +271,8 @@ "Now let's try to add two numbers using JavaScript.", "JavaScript uses the + symbol as addition operation when placed between two numbers.", "", - "Example
5 + 10 = 15
", + "Example", + "
5 + 10 = 15
", "", "

Instructions

", "Change the 0 so that sum will equal 20." @@ -296,7 +298,8 @@ "We can also subtract one number from another.", "JavaScript uses the - symbol for subtraction.", "", - "Example
12 - 6 = 6
", + "Example", + "
12 - 6 = 6
", "", "

Instructions

", "Change the 0 so that difference will equal 12." @@ -326,7 +329,8 @@ "We can also multiply one number by another.", "JavaScript uses the * symbol for multiplication of two numbers.", "", - "Example
13 * 13 = 169
", + "Example", + "
13 * 13 = 169
", "", "

Instructions

", "Change the 0 so that product will equal 80." @@ -356,7 +360,8 @@ "We can also divide one number by another.", "JavaScript uses the / symbol for division.", "", - "Example
16 / 2 = 8
", + "Example", + "
16 / 2 = 8
", "", "

Instructions

", "Change the 0 so that quotient will equal to 2." @@ -531,7 +536,8 @@ "title": "Find a Remainder with Modulus", "description": [ "The modulus operator % gives the remainder of the division of two numbers.", - "Example
5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)
", + "Example", + "
5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)
", "Usage", "In Maths, a number can be checked even or odd by checking the remainder of division of the number by 2. ", "
17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)
", @@ -1151,7 +1157,8 @@ "For example, the character at index 0 in the word \"Charles\" is \"C\". So if var firstName = \"Charles\", you can get the value of the first letter of the string by using firstName[0].", "

Instructions

", "Use bracket notation to find the first character in the lastName variable and assign it to firstLetterOfLastName.", - "Hint
Try looking at the firstLetterOfFirstName variable declaration if you get stuck." + "Hint", + "
Try looking at the firstLetterOfFirstName variable declaration if you get stuck." ], "tests": [ "assert((function(){if(typeof(firstLetterOfLastName) !== \"undefined\" && editor.getValue().match(/\\[0\\]/gi) && typeof(firstLetterOfLastName) === \"string\" && firstLetterOfLastName === \"L\"){return true;}else{return false;}})(), 'message: The firstLetterOfLastName variable should have the value of L.');" @@ -1234,7 +1241,8 @@ "Remember that computers start counting at 0, so the first character is actually the zeroth character.", "

Instructions

", "Let's try to set thirdLetterOfLastName to equal the third letter of the lastName variable.", - "Hint
Try looking at the secondLetterOfFirstName variable declaration if you get stuck." + "Hint", + "
Try looking at the secondLetterOfFirstName variable declaration if you get stuck." ], "tests": [ "assert(thirdLetterOfLastName === 'v', 'message: The thirdLetterOfLastName variable should have the value of v.');" @@ -1270,7 +1278,8 @@ "For example, if var firstName = \"Charles\", you can get the value of the last letter of the string by using firstName[firstName.length - 1].", "

Instructions

", "Use bracket notation to find the last character in the lastName variable.", - "Hint
Try looking at the lastLetterOfFirstName variable declaration if you get stuck." + "Hint", + "
Try looking at the lastLetterOfFirstName variable declaration if you get stuck." ], "tests": [ "assert(lastLetterOfLastName === \"e\", 'message: lastLetterOfLastName should be \"e\".');", @@ -1307,7 +1316,8 @@ "For example, you can get the value of the third-to-last letter of the var firstName = \"Charles\" string by using firstName[firstName.length - 3]", "

Instructions

", "Use bracket notation to find the second-to-last character in the lastName string.", - " Hint
Try looking at the thirdToLastLetterOfFirstName variable declaration if you get stuck." + " Hint", + "
Try looking at the thirdToLastLetterOfFirstName variable declaration if you get stuck." ], "tests": [ "assert(secondToLastLetterOfLastName === 'c', 'message: secondToLastLetterOfLastName should be \"c\".');", @@ -1390,10 +1400,11 @@ "title": "Store Multiple Values in one Variable using JavaScript Arrays", "description": [ "With JavaScript array variables, we can store several pieces of data in one place.", - "You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
var sandwich = [\"peanut butter\", \"jelly\", \"bread\"].", + "You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
var sandwich = [\"peanut butter\", \"jelly\", \"bread\"].", "

Instructions

", "Create a new array called myArray that contains both a string and a number (in that order).", - "Hint
Refer to the example code in the text editor if you get stuck." + "Hint", + "
Refer to the example code in the text editor if you get stuck." ], "tests": [ "assert(typeof(myArray) == 'object', 'message: myArray should be an array.');", @@ -1450,11 +1461,9 @@ "title": "Access Array Data with Indexes", "description": [ "We can access the data inside arrays using indexes.", - "Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use zero-based indexing, so the first element in an array is element 0", - "For example:", - "var array = [1,2,3];", - "array[0]; //equals 1", - "var data = array[1];", + "Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use zero-based indexing, so the first element in an array is element 0.", + "Example", + "
var array = [1,2,3];
array[0]; // equals 1
var data = array[1]; // equals 2
", "

Instructions

", "Create a variable called myData and set it to equal the first value of myArray." ], @@ -1487,9 +1496,8 @@ "title": "Modify Array Data With Indexes", "description": [ "Unlike strings, the entries of arrays are mutable and can be changed freely.", - "For example:", - "var ourArray = [3,2,1];", - "ourArray[0] = 1; // equals [1,2,1]", + "Example", + "
var ourArray = [3,2,1];
ourArray[0] = 1; // equals [1,2,1]
", "

Instructions

", "Modify the data stored at index 0 of myArray to a value of 3." ], @@ -1524,7 +1532,8 @@ "title": "Access Multi-Dimensional Arrays With Indexes", "description": [ "One way to think of a multi-dimensional array, is as an array of arrays. When you use brackets to access your array, the first set of bracket refers to the entries in outer-most array, and each subsequent level of brackets refers to the next level of entry in.", - "Example:
var arr = [
[1,2,3],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
arr[0]; // equals [1,2,3]
arr[1][2]; // equals 6
arr[3][0][1]; // equals 11
", + "Example", + "
var arr = [
[1,2,3],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
arr[0]; // equals [1,2,3]
arr[1][2]; // equals 6
arr[3][0][1]; // equals 11
", "

Instructions

", "Read from myArray using bracket notation so that myData is equal to 8" ], @@ -1771,7 +1780,7 @@ "description": [ "In JavaScript, we can divide up our code into reusable parts called functions.", "Here's an example of a function:", - "
function functionName() {
console.log(\"Hello World\");
}
", + "
function functionName() {
console.log(\"Hello World\");
}
", "You can call or invoke this function by using its name followed by parentheses, like this:", "functionName();", "Each time the function is called it will print out the message \"Hello World\" on the dev console. All of the code between the curly braces will be executed every time the function is called.", @@ -2026,7 +2035,7 @@ "description": [ "It is possible to have both a local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.", "In this example:", - "
var someVar = \"Hat\";
function myFun() {
var someVar = \"Head\";
return someVar;
}
", + "
var someVar = \"Hat\";
function myFun() {
var someVar = \"Head\";
return someVar;
}
", "The function myFun will return \"Head\" because the local version of the variable is present.", "

Instructions

", "Add a local variable to myFunction to override the value of outerWear with \"sweater\"." @@ -2072,8 +2081,8 @@ "title": "Return a Value from a Function with Return", "description": [ "We can pass values into a function with arguments. You can use a return statement to send a value back out of a function.", - "For Example:", - "
function plusThree(num) {
return num + 3;
}
var answer = plusThree(5); // 8
", + "Example", + "
function plusThree(num) {
return num + 3;
}
var answer = plusThree(5); // 8
", "plusThree takes an argument for num and returns a value equal to num + 3.", "

Instructions

", "Create a function timesFive that accepts one argument, multiplies it by 5, and returns the new value." @@ -2224,7 +2233,7 @@ "description": [ "We can use if statements in JavaScript to only execute code if a certain condition is met.", "if statements require a boolean condition to evaluate. If the boolean evaluates to true, the statement inside the curly braces executes. If it evaluates to false, the code will not execute.", - "For example:", + "Example", "
function test(myVal) {
if (myVal > 10) {
return \"Greater Than\";
}
return \"Not Greater Than\";
}
", "If myVal is greater than 10, the function will return \"Greater Than\". If it is not, the function will return \"Not Greater Than\".", "

Instructions

", @@ -2325,7 +2334,8 @@ "title": "Comparison with the Strict Equality Operator", "description": [ "Strict equality (===) is the counterpart to the equality operator (==). Unlike the equality operator, strict equality tests both the type and value of the compared elements.", - "Examples
3 === 3 // true
3 === '3' // false
", + "Examples", + "
3 === 3 // true
3 === '3' // false
", "In the second example, 3 is a Number type and '3' is a String type.", "

Instructions

", "Use strict equality operator in if statement so the function will return \"Equal\" when val is strictly equal to 7" @@ -2370,7 +2380,8 @@ "title": "Comparison with the Inequality Operator", "description": [ "The inequality operator (!=) is the opposite of the equality operator. It means \"Not Equal\" and returns false where equality would return true and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing.", - "Examples
1 != 2 // true
1 != \"1\" // false
1 != '1' // false
1 != true // false
0 != false // false
", + "Examples", + "
1 != 2 // true
1 != \"1\" // false
1 != '1' // false
1 != true // false
0 != false // false
", "

Instructions

", "Add the inequality operator != in the if statement so that the function will return \"Not Equal\" when val is not equivalent to 99" ], @@ -2416,7 +2427,8 @@ "title": "Comparison with the Strict Inequality Operator", "description": [ "The inequality operator (!==) is the opposite of the strict equality operator. It means \"Strictly Not Equal\" and returns false where strict equality would return true and vice versa. Strict inequality will not convert data types.", - "Examples
3 !== 3 // false
3 !== '3' // true
4 !== 3 // true
", + "Examples", + "
3 !== 3 // false
3 !== '3' // true
4 !== 3 // true
", "

Instructions

", "Add the strict inequality operator to the if statement so the function will return \"Not Equal\" when val is not strictly equal to 17" ], @@ -2466,7 +2478,8 @@ "title": "Comparison with the Greater Than Operator", "description": [ "The greater than operator (>) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns true. Otherwise, it returns false.
Like the equality operator, greater than operator will convert data types of values while comparing.", - "Examples
5 > 3 // true
7 > '3' // true
2 > 3 // false
'1' > 9 // false
", + "Examples", + "
5 > 3 // true
7 > '3' // true
2 > 3 // false
'1' > 9 // false
", "

Instructions

", "Add the greater than operator to the indicated lines so that the return statements make sense." ], @@ -2520,7 +2533,8 @@ "title": "Comparison with the Greater Than Equal To Operator", "description": [ "The greater than equal to operator (>=) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns true. Otherwise, it returns false.
Like the equality operator, greater than equal to operator will convert data types while comparing.", - "Examples
6 >= 6 // true
7 >= '3' // true
2 >= 3 // false
'7' >= 9 // false
", + "Examples", + "
6 >= 6 // true
7 >= '3' // true
2 >= 3 // false
'7' >= 9 // false
", "

Instructions

", "Add the greater than equal to operator to the indicated lines so that the return statements make sense." ], @@ -2577,7 +2591,8 @@ "title": "Comparison with the Less Than Operator", "description": [ "The less than operator (<) compares the values of two numbers. If the number to the left is less than the number to the right, it returns true. Otherwise, it returns false. Like the equality operator, less than operator converts data types while comparing.", - "Examples
2 < 5 // true
'3' < 7 // true
5 < 5 // false
3 < 2 // false
'8' < 4 // false
", + "Examples", + "
2 < 5 // true
'3' < 7 // true
5 < 5 // false
3 < 2 // false
'8' < 4 // false
", "

Instructions

", "Add the less than operator to the indicated lines so that the return statements make sense." ], @@ -2633,7 +2648,8 @@ "title": "Comparison with the Less Than Equal To Operator", "description": [ "The less than equal to operator (<=) compares the values of two numbers. If the number to the left is less than or equl the number to the right, it returns true. If the number on the left is greater than the number on the right, it returns false. Like the equality operator, less than equal to converts data types.", - "Examples
4 <= 5 // true
'7' <= 7 // true
5 <= 5 // true
3 <= 2 // false
'8' <= 4 // false
", + "Examples", + "
4 <= 5 // true
'7' <= 7 // true
5 <= 5 // true
3 <= 2 // false
'8' <= 4 // false
", "

Instructions

", "Add the less than equal to operator to the indicated lines so that the return statements make sense." ], @@ -2881,7 +2897,7 @@ "title": "Introducing Else Statements", "description": [ "When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.", - "
if (num > 10) {
return \"Bigger than 10\";
} else {
return \"10 or Less\";
}
", + "
if (num > 10) {
return \"Bigger than 10\";
} else {
return \"10 or Less\";
}
", "

Instructions

", "Combine the if statements into a single statement." ], @@ -3180,11 +3196,12 @@ "title": "Multiple Identical Options in Switch Statements", "description": [ "If the break statement is ommitted from a switch statement case, the following case statement(s) are executed unless a break is encountered. If you have mutiple inputs with the same output, you can represent them in a switch statement like this:", - "
switch(val) {
case 1:
case 2:
case 3:
result = \"1, 2, or 3\";
break;
case 4:
result = \"4 alone\";
}
", + "
switch(val) {
case 1:
case 2:
case 3:
result = \"1, 2, or 3\";
break;
case 4:
result = \"4 alone\";
}
", "Cases for 1, 2, and 3 will all produce the same result.", "

Instructions

", - "Write a switch statement to set answer for the following ranges:
1-3 - \"Low\"
4-6 - \"Mid\"
7-9 - \"High\"", - "Note
You will need to have a case statement for each number in the range." + "Write a switch statement to set answer for the following ranges:
1-3 - \"Low\"
4-6 - \"Mid\"
7-9 - \"High\"", + "Note", + "You will need to have a case statement for each number in the range." ], "releasedOn": "11/27/2015", "tests": [ @@ -3333,9 +3350,9 @@ "description": [ "You may recall from Comparison with the Equality Operator that all comparison operators return a boolean true or false value.", "A common anti-pattern is to use an if/else statement to do a comparison and then return true/false:", - "
function isEqual(a,b) {
if(a === b) {
return true;
} else {
return false;
}
}
", + "
function isEqual(a,b) {
if(a === b) {
return true;
} else {
return false;
}
}
", "Since === returns true or false, we can simply return the result of the comparion:", - "
function isEqual(a,b) {
return a === b;
}
", + "
function isEqual(a,b) {
return a === b;
}
", "

Instructions

", "Fix the function isLess to remove the if/else statements." ], @@ -3376,51 +3393,47 @@ "id": "56533eb9ac21ba0edf2244c4", "title": "Return Early Pattern for Functions", "description": [ - "When a return statement is reached, the execution of the current function stops at that point. " + "When a return statement is reached, the execution of the current function stops and control returns to the calling location.", + "Example", + "
function myFun() {
console.log(\"Hello\");
return \"World\";
console.log(\"byebye\")
}
myFun();
", + "The above outputs \"Hello\" to the console, returns \"World\", but \"byebye\" is never output, because the function exits at the return statement.", + "

Instructions

", + "Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined." ], "releasedOn": "11/27/2015", "tests": [ - "assert(1===1, 'message: message here');" + "assert(typeof abTest(2,2) === 'number' , 'message: abTest(2,2) should return a number');", + "assert(abTest(2,2) === 8 , 'message: abTest(2,2) should return 8');", + "assert(abTest(-2,2) === undefined , 'message: abTest(-2,2) should return undefined');", + "assert(abTest(2,-2) === undefined , 'message: abTest(2,-2) should return undefined');", + "assert(abTest(2,8) === 18 , 'message: abTest(2,8) should return 18');", + "assert(abTest(3,3) === 12 , 'message: abTest(3,3) should return 12');" ], "challengeSeed": [ + "// Setup", + "function abTest(a, b) {", + " // Only change code below this line", + " ", + " ", + " ", + " // Only change code above this line", "", + " return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));", + "}", "", - "" + "// Change values below to test your code", + "abTest(2,2);" ], "tail": [ "" ], "solutions": [ - "" - ], - "type": "waypoint", - "challengeType": "1", - "nameCn": "", - "nameFr": "", - "nameRu": "", - "nameEs": "", - "namePt": "" - }, - { - "id": "56533eb9ac21ba0edf2244c5", - "title": "Optional Arguments for Functions", - "description": [ - "" - ], - "releasedOn": "11/27/2015", - "tests": [ - "assert(1===1, 'message: message here');" - ], - "challengeSeed": [ - "", - "", - "" - ], - "tail": [ - "" - ], - "solutions": [ - "" + "function abTest(a, b) {", + " if(a < 0 || b < 0) {", + " return undefined;", + " } ", + " return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));", + "}" ], "type": "waypoint", "challengeType": "1", @@ -3439,7 +3452,7 @@ "
ValueCards
+12, 3, 4, 5, 6
07, 8, 9
-110, 'J', 'Q', 'K','A'
", "You will write a card counting function. It will recieve a card parameter and increment or decrement the global count variable according to the card's value (see table). The function will then return the current count and the string \"Bet\" if the count if positive, or \"Hold\" if the count is zero or negative.", "Example Output", - "-3 Hold
5 Bet" + "-3 Hold
5 Bet" ], "releasedOn": "11/27/2015", "tests": [ @@ -3503,7 +3516,7 @@ "You may have heard the term object before.", "Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.", "Here's a sample object:", - "
var cat = {
\"name\": \"Whiskers\",
\"legs\": 4,
\"tails\": 1,
\"enemies\": [\"Water\", \"Dogs\"]
};
", + "
var cat = {
\"name\": \"Whiskers\",
\"legs\": 4,
\"tails\": 1,
\"enemies\": [\"Water\", \"Dogs\"]
};
", "Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.", "

Instructions

", "Make an object that represents a dog called myDog which contains the properties \"name\" (a string), \"legs\", \"tails\" and \"friends\".", @@ -3549,7 +3562,7 @@ "There are two ways to access the properties of an object: the dot operator (.) and bracket notation ([]), similar to an array.", "The dot operator is what you use when you know the name of the property you're trying to access ahead of time.", "Here is a sample of using the . to read an object property:", - "
var myObj = {
prop1: \"val1\",
prop2: \"val2\"
};
myObj.prop1; // val1
myObj.prop2; // val2
", + "
var myObj = {
prop1: \"val1\",
prop2: \"val2\"
};
myObj.prop1; // val1
myObj.prop2; // val2
", "

Instructions

", "Read the values of the properties hat and shirt of testObj using dot notation." ], @@ -3600,7 +3613,7 @@ "description": [ "The second way to access the properties of an objectis bracket notation ([]). If the property of the object you are trying to access has a space in it, you will need to use bracket notation.", "Here is a sample of using bracket notation to read an object property:", - "
var myObj = {
\"Space Name\": \"Kirk\",
\"More Space\": \"Spock\"
};
myObj[\"Space Name\"]; // Kirk
myObj['More Space']; // Spock
", + "
var myObj = {
\"Space Name\": \"Kirk\",
\"More Space\": \"Spock\"
};
myObj[\"Space Name\"]; // Kirk
myObj['More Space']; // Spock
", "Note that property names with spaces in them must be in quotes (single or double).", "

Instructions

", "Read the values of the properties \"an entree\" and \"the drink\" of testObj using bracket notation." @@ -3652,7 +3665,7 @@ "description": [ "Another use of bracket notation on objects is to use a variable to access a property. This can be very useful for itterating through lists of object properties or for doing lookups.", "Here is an example of using a variable to access a property:", - "
var someProp = \"propName\";
var myObj = {
propName: \"Some Value\"
}
myObj[someProp]; // \"Some Value\"
", + "
var someProp = \"propName\";
var myObj = {
propName: \"Some Value\"
}
myObj[someProp]; // \"Some Value\"
", "Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name", "

Instructions

", "Use the playerNumber variable to lookup player 16 in testObj using bracket notation." @@ -3697,7 +3710,7 @@ "description": [ "After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.", "For example, let's look at ourDog:", - "
var ourDog = {
\"name\": \"Camper\",
\"legs\": 4,
\"tails\": 1,
\"friends\": [\"everything!\"]
};
", + "
var ourDog = {
\"name\": \"Camper\",
\"legs\": 4,
\"tails\": 1,
\"friends\": [\"everything!\"]
};
", "Since he's a particularly happy dog, let's change his name to \"Happy Camper\". Here's how we update his object's name property:", "ourDog.name = \"Happy Camper\"; or", "outDog[\"name\"] = \"Happy Camper\";", @@ -3853,7 +3866,7 @@ "description": [ "Objects can be thought of as a key/value storage, like a dictonary. If you have tabular data, you can use an object to \"lookup\" values rather than a switch statement or an if...else chain. This is most useful when you know that your input data is limited to a certain range.", "Here is an example of a simple reverse alphabet lookup:", - "
var alpha = {
1:\"Z\"
2:\"Y\"
3:\"X\"
...
4:\"W\"
24:\"C\"
25:\"B\"
26:\"A\"
};
alpha[2]; // \"Y\"
alpha[24]; // \"C\"
", + "
var alpha = {
1:\"Z\"
2:\"Y\"
3:\"X\"
...
4:\"W\"
24:\"C\"
25:\"B\"
26:\"A\"
};
alpha[2]; // \"Y\"
alpha[24]; // \"C\"
", "

Instructions

", "Convert the switch statement into a lookup table called lookup. Use it to lookup val and return the associated string." ], @@ -3950,8 +3963,8 @@ "title": "Testing Objects for Properties", "description": [ "Sometimes it is useful to check of the property of a given object exists or not. We can use the .hasOwnProperty([propname]) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.", - "Example:", - "
var myObj = {
top: \"hat\",
bottom: \"pants\"
};
myObj.hasOwnProperty(\"hat\"); // true
myObj.hasOwnProperty(\"middle\"); // false
", + "Example", + "
var myObj = {
top: \"hat\",
bottom: \"pants\"
};
myObj.hasOwnProperty(\"hat\"); // true
myObj.hasOwnProperty(\"middle\"); // false
", "

Instructions

", " Modify function checkObj to test myObj for checkProp. If the property is found, return that property's value. If not return \"Not Found\"." ], @@ -4003,7 +4016,7 @@ "description": [ "JavaScript Object Notation or JSON uses the format of Javascript Objects to store data. JSON is flexible becuase it allows for data structures with arbitrary combinations of strings, numbers, booleans, arrays, and objects. ", "Here is an example of a JSON object:", - "
var ourMusic = [
{
\"artist\": \"Daft Punk\",
\"title\": \"Homework\",
\"release_year\": 1997,
\"formats\": [
\"CD\",
\"Cassette\",
\"LP\" ],
\"gold\": true
}
];
", + "
var ourMusic = [
{
\"artist\": \"Daft Punk\",
\"title\": \"Homework\",
\"release_year\": 1997,
\"formats\": [
\"CD\",
\"Cassette\",
\"LP\" ],
\"gold\": true
}
];
", "This is an array of objects and the object has various peices of metadata about an album. It also has a nested array of formats. Additional album records could be added to the top level array.", "

Instructions

", "Add a new album to the myMusic JSON object. Add artist and title strings, release_year year, and a formats array of strings." @@ -4080,7 +4093,7 @@ "description": [ "The properties and sub-properties of JSON objects can be accessed by chaining together the dot or bracket notation.", "Here is a nested JSON Object:", - "
var ourStorage = {
\"desk\": {
\"drawer\": \"stapler\"
},
\"cabinet\": {
\"top drawer\": {
\"folder1\": \"a file\",
\"folder2\": \"secrets\"
},
\"bottom drawer\": \"soda\"
}
}
ourStorage.cabinet[\"top drawer\"].folder2; // \"secrets\"
ourStoage.desk.drawer; // \"stapler\"
", + "
var ourStorage = {
\"desk\": {
\"drawer\": \"stapler\"
},
\"cabinet\": {
\"top drawer\": {
\"folder1\": \"a file\",
\"folder2\": \"secrets\"
},
\"bottom drawer\": \"soda\"
}
}
ourStorage.cabinet[\"top drawer\"].folder2; // \"secrets\"
ourStoage.desk.drawer; // \"stapler\"
", "

Instructions

", "Access the myStorage JSON object to retrieve the contents of the glove box. Only use object notation for properties with a space in their name." ], @@ -4139,7 +4152,7 @@ "description": [ "As we have seen in earlier examples, JSON objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays.", "Here is an example of how to access a nested array:", - "
var ourPets = {
\"cats\": [
\"Meowzer\",
\"Fluffy\",
\"Kit-Cat\"
],
\"dogs:\" [
\"Spot\",
\"Bowser\",
\"Frankie\"
]
};
ourPets.cats[1]; // \"Fluffy\"
ourPets.dogs[0]; // \"Spot\"
", + "
var ourPets = {
\"cats\": [
\"Meowzer\",
\"Fluffy\",
\"Kit-Cat\"
],
\"dogs:\" [
\"Spot\",
\"Bowser\",
\"Frankie\"
]
};
ourPets.cats[1]; // \"Fluffy\"
ourPets.dogs[0]; // \"Spot\"
", "

Instructions

", "Retrieve the second tree using object dot and array bracket notation." ], @@ -4343,7 +4356,7 @@ "The condition statement is evaluated at the beginning of every loop iteration and will continue as long as it evalutes to true. When condition is false at the start of the iteration, the loop will stop executing. This means if condition starts as false, your loop will never execute.", "The final-expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.", "In the following example we initialize with i = 0 and iterate while our condition i < 5 is true. We'll increment i by 1 in each loop iteration with i++ as our final-expression.", - "
var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
}
", + "
var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
}
", "ourArray will now contain [0,1,2,3,4].", "

Instructions

", "Use a for loop to work to push the values 1 through 5 onto myArray." @@ -4379,7 +4392,7 @@ "description": [ "For loops don't have to iterate one at a time. By changing our final-expression, we can count by even numbers.", "We'll start at i = 0 and loop while i < 10. We'll increment i by 2 each loop with i += 2.", - "
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
", + "
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
", "ourArray will now contain [0,2,4,6,8].", "Let's change our initialization so we can count by odd numbers.", "

Instructions

", @@ -4417,7 +4430,7 @@ "A for loop can also count backwards, so long as we can define the right conditions.", "In order to count backwards by twos, we'll need to change our initialization, condition, and final-expression.", "We'll start at i = 10 and loop while i > 0. We'll decrement i by 2 each loop with i -= 2.", - "
var ourArray = [];
for (var i=10; i > 0; i-=2) {
ourArray.push(i);
}
", + "
var ourArray = [];
for (var i=10; i > 0; i-=2) {
ourArray.push(i);
}
", "ourArray will now contain [10,8,6,4,2].", "Let's change our initialization and final-expression so we can count backward by twos by odd numbers.", "

Instructions

", @@ -4452,7 +4465,7 @@ "title": "Iterate Through an Array with a For Loop", "description": [ "A common task in Javascript is to iterate through the contents of an array. One way to do that is with a for loop. This code will output each element of the array arr to the console:", - "
var arr = [10,9,8,7,6];
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}
", + "
var arr = [10,9,8,7,6];
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}
", "Remember that Arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1.", "

Instructions

", "Create a variable total. Use a for loop to add each element of myArr to total." @@ -4503,7 +4516,7 @@ "title": "Nesting For Loops", "description": [ "If you have a multi-dimensional array, you can use the same logic as the prior waypoint to loop through both the array and any sub-arrays. Here is an example:", - "
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; k < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
", + "
var arr = [
[1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; k < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
", "This outputs each sub-element in arr one at a time. Note that for the inner loop we are checking the .length of arr[i], since arr[i] is itself an array.", "

Instructions

", "Modify function multiplyAll so that it multiplies product by each number in the subarrays of arr" @@ -4559,7 +4572,7 @@ "description": [ "You can run the same code multiple times by using a loop.", "Another type of JavaScript loop is called a \"while loop\", because it runs \"while\" something is true and stops once that something is no longer true.", - "
var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}
", + "
var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}
", "Let's try getting a while loop to work by pushing values to an array.", "

Instructions

", "Push the numbers 0 through 4 to myArray using a while loop." @@ -4673,8 +4686,9 @@ " return 0;", "", " // Only change code above this line.", - "}", - "", + "}" + ], + "tail": [ "(function(){return myFunction();})();" ], "type": "waypoint", @@ -4709,8 +4723,6 @@ " // Only change code below this line.", "", " return Math.random();", - "", - " // Only change code above this line.", "}" ], "tail": [ @@ -4757,11 +4769,9 @@ "", " return;", "", - "}", - "", - "// Only change code above this line.", - "", - "", + "}" + ], + "tail": [ "(function(){return myFunction();})();" ], "type": "waypoint", @@ -4785,8 +4795,13 @@ "assert(test==2, 'message: Your regular expression should find two occurrences of the word and.');", "assert(editor.getValue().match(/\\/and\\/gi/), 'message: Use regular expressions to find the word and in testString.');" ], + "head": [ + "" + ], "challengeSeed": [ + "// Setup", "var test = (function() {", + " // Example", " var testString = \"Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it.\";", " var expressionToGetSoftware = /software/gi;", "", @@ -4794,11 +4809,13 @@ "", " var expression = /./gi;", "", - " // Only change code above this line.", - "", + " // Only change code above this line", " return testString.match(expression).length;", "})();(function(){return test;})();" ], + "tail": [ + "" + ], "type": "waypoint", "challengeType": "1" }, @@ -4816,6 +4833,9 @@ "assert(editor.getValue().match(/\\/\\\\d\\+\\//g), 'message: Use the /\\d+/g regular expression to find the numbers in testString.');", "assert(test === 2, 'message: Your regular expression should find two numbers in testString.');" ], + "head": [ + "var test = (function() {" + ], "challengeSeed": [ "var test = (function() {", "", @@ -4824,11 +4844,10 @@ " // Only change code below this line.", "", " var expression = /.+/g;", - "", - " // Only change code above this line.", - "", + "" + ], + "tail": [ " return testString.match(expression).length;", - "", "})();(function(){return test;})();" ], "type": "waypoint", @@ -4848,16 +4867,18 @@ "assert(editor.getValue().match(/\\/\\\\s\\+\\//g), 'message: Use the /\\s+/g regular expression to find the spaces in testString.');", "assert(test === 7, 'message: Your regular expression should find seven spaces in testString.');" ], + "head": [ + "var test = (function() {" + ], "challengeSeed": [ - "var test = (function(){", " var testString = \"How many spaces are there in this sentence?\";", "", " // Only change code below this line.", "", " var expression = /.+/g;", - "", - " // Only change code above this line.", - "", + "" + ], + "tail": [ " return testString.match(expression).length;", "", "})();(function(){return test;})();" @@ -4877,16 +4898,18 @@ "assert(editor.getValue().match(/\\/\\\\S\\/g/g), 'message: Use the /\\S/g regular expression to find non-space characters in testString.');", "assert(test === 49, 'message: Your regular expression should find forty nine non-space characters in the testString.');" ], + "head": [ + "var test = (function() {" + ], "challengeSeed": [ - "var test = (function(){", " var testString = \"How many non-space characters are there in this sentence?\";", "", " // Only change code below this line.", "", " var expression = /./g;", - "", - " // Only change code above this line.", - "", + "" + ], + "tail": [ " return testString.match(expression).length;", "})();(function(){return test;})();" ],