finish JavaScript and OOP sections for QA

This commit is contained in:
Quincy Larson
2015-10-28 01:15:54 -07:00
parent 419f6507d0
commit eef1c5136a
2 changed files with 94 additions and 62 deletions

View File

@@ -955,7 +955,6 @@
" // Only change code above this line.",
"}",
"",
"// We use this function to show you the value of your variable in your output box.",
"(function(){return myFunction();})();"
],
"type": "waypoint",
@@ -965,8 +964,8 @@
"id": "cf1111c1c12feddfaeb1bdef",
"title": "Generate Random Whole Numbers with JavaScript",
"description": [
"It's great that we can create random decimal numbers, but it's even more useful if we use it to generate random whole numbers.",
"First, let's use <code>Math.random()</code> to create a random decimal.",
"It's great that we can generate random decimal numbers, but it's even more useful if we use it to generate random whole numbers.",
"First, let's use <code>Math.random()</code> to generate a random decimal.",
"Then let's multiply this random decimal by 20.",
"Finally, let's use another function, <code>Math.floor()</code> to round the number down to its nearest whole number.",
"This technique will gives us a whole number between 0 and 19.",
@@ -974,11 +973,11 @@
"Putting everything together, this is what our code looks like:",
"<code>Math.floor(Math.random() * 20);</code>",
"See how <code>Math.floor</code> takes <code>(Math.random() * 20)</code> as its argument? That's right - you can pass a function to another function as an argument."
"Let's use this technique to create and return a random whole number between 0 and 9."
"Let's use this technique to generate and return a random whole number between 0 and 9."
],
"tests": [
"assert(typeof(myFunction()) === \"number\", 'message: The result of <code>myFunction</code> should be a number.');",
"assert(editor.getValue().match(/Math.random/g), 'message: You should be using Math.random to create a random number.');",
"assert(editor.getValue().match(/Math.random/g), 'message: You should be using Math.random to generate a random number.');",
"assert(editor.getValue().match(/\\(\\s*?Math.random\\s*?\\(\\s*?\\)\\s*?\\*\\s*?10\\s*?\\)/g) || editor.getValue().match(/\\(\\s*?10\\s*?\\*\\s*?Math.random\\s*?\\(\\s*?\\)\\s*?\\)/g), 'message: You should have multiplied the result of <code>Math.random</code> by 10 to make it a number that is between zero and nine.');",
"assert(editor.getValue().match(/Math.floor/g), 'message: You should use <code>Math.floor</code> to remove the decimal part of the number.');"
],
@@ -994,7 +993,6 @@
" // Only change code above this line.",
"}",
"",
"// We use this function to show you the value of your variable in your output box.",
"(function(){return myFunction();})();"
],
"type": "waypoint",
@@ -1004,28 +1002,42 @@
"id": "cf1111c1c12feddfaeb2bdef",
"title": "Generate Random Whole Numbers within a Range",
"description": [
"We can use a certain mathematical expression to get a random number between two numbers.",
"Instead of generating a random number between zero and a given number like we did before, we can generate a random number that falls within a range of two specific numbers.",
"To do this, we'll define a minimum number <code>min</code> and a maximum number <code>max</code>."
"Here's the formula we'll use. Take a moment to read and try to understand what this code is doing.",
"<code>Math.floor(Math.random() * (max - min + 1)) + min</code>",
"By using this, we can control the output of a random number."
"Define two variables: <code>myMin</code> and </code>myMax</code>, and set them both equal to numbers.",
"Then create a function called <code>myFunction</code> that returns a random number that's greater than or equal to <code>myMin</code>, and is less than <code>myMax</code>."
],
"tests": [
"assert(myFunction() >= min, 'message: The random number generated by <code>myFunction</code> should be greater than or equal to the minimum number.');",
"assert(myFunction() <= max, 'message: The random number generated by <code>myFunction</code> should be less than or equal to the maximum number.');",
"assert(myFunction() >= myMin, 'message: The random number generated by <code>myFunction</code> should be greater than or equal to your minimum number, <code>myMin</code>.');",
"assert(myFunction() <= myMax, 'message: The random number generated by <code>myFunction</code> should be less than or equal to your maximum number, <code>myMax</code>.');",
"assert(myFunction() % 1 === 0 , 'message: The random number generated by <code>myFunction</code> should be an integer, not a decimal.');",
"assert((function(){if(editor.getValue().match(/max/g).length >= 3 && editor.getValue().match(/min/g).length >= 4 && editor.getValue().match(/Math.floor/g) && editor.getValue().match(/Math.random/g)){return true;}else{return false;}})(), 'message: You should be using the function given in the description to calculate the random in number in a range.');"
"assert((function(){if(editor.getValue().match(/myMax/g).length >= 3 && editor.getValue().match(/myMin/g).length >= 4 && editor.getValue().match(/Math.floor/g) && editor.getValue().match(/Math.random/g)){return true;}else{return false;}})(), 'message: You should be using a function called <code>myFunction()</code> to calculate your random number in your range.');"
],
"challengeSeed": [
"var min = 1;",
"var max = 9;",
"function myFunction() {",
"var ourMin = 1;",
"",
"var ourMax = 9;",
"",
"function ourFunction() {",
"",
" return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;",
"",
"}",
"",
" // Only change code below this line.",
"",
" return Math.random();",
"",
"",
"",
"",
"",
"",
"",
"",
"// Only change code above this line.",
"",
"}",
"",
"(function(){return myFunction();})();"
],
@@ -1039,7 +1051,7 @@
"We can use <code>if</code> statements in JavaScript to only execute code if a certain condition is met.",
"<code>if</code> statements require some sort of boolean condition to evaluate.",
"For example:",
"<code> if (1 === 2) {</code>",
"<code>if (1 === 2) {</code>",
"<code>&thinsp;&thinsp;return true;</code>",
"<code>} else {</code>",
"<code>&thinsp;&thinsp;return false;</code>",
@@ -1056,16 +1068,15 @@
"challengeSeed": [
"var flip = Math.floor(Math.random() * (1 - 0 + 1)) + 0;",
"function myFunction(){",
" // Create an if-else statement here to return \"heads\" if flip is 0. Otherwise return \"tails\".",
"",
" // Only change code below this line.",
"",
"",
"",
" // Only change code above this line.",
"",
"}",
"",
"// We use this function to show you the value of your variable in your output box.",
"var result = myFunction();if(typeof(flip) !== \"undefined\" && typeof(flip) === \"number\" && typeof(result) !== \"undefined\" && typeof(result) === \"string\"){(function(y,z){return 'flip = ' + y.toString() + ', text = ' + z;})(flip, result);}"
],
"type": "waypoint",
@@ -1082,22 +1093,24 @@
"<code>g</code> means that we want to search the entire string for this pattern instead of just the first match.",
"<code>i</code> means that we want to ignore the case (uppercase or lowercase) when searching for the pattern.",
"<code>Regular expressions</code> are written by surrounding the pattern with <code>/</code> symbols.",
"Let's try selecting all the occurrences of the word <code>and</code> in the string <code>Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it</code>. We can do this by replacing the <code>.</code> part of our regular expression with the current <code>regular expression</code> with the word <code>and</code>."
"Let's try selecting all the occurrences of the word <code>and</code> in the string <code>Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it</code>.",
"We can do this by replacing the <code>.</code> part of our regular expression with the current <code>regular expression</code> with the word <code>and</code>."
],
"tests": [
"assert(test==2, 'message: Your <code>regular expression</code> should find two occurrences of the word <code>and</code>.');",
"assert(editor.getValue().match(/\\/and\\/gi/), 'message: You should have used <code>regular expressions</code> to find the word <code>and</code>.');"
"assert(editor.getValue().match(/\\/and\\/gi/), 'message: Use <code>regular expressions</code> to find the word <code>and</code> in <code>testString</code>.');"
],
"challengeSeed": [
"var test = (function() {",
" var testString = \"Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it.\";",
" var expressionToGetSoftware = /software/gi;",
"",
" // Only change code below this line.",
"",
" var expression = /./gi;",
"",
" // Only change code above this line.",
" // We use this function to show you the value of your variable in your output box.",
"",
" return testString.match(expression).length;",
"})();(function(){return test;})();"
],
@@ -1115,11 +1128,12 @@
"Use the <code>\\d</code> selector to select the number of numbers in the string, allowing for the possibility of multi-digit numbers."
],
"tests": [
"assert(test === 2, 'message: Your RegEx should have found two numbers in the <code>testString</code>.');",
"assert(editor.getValue().match(/\\/\\\\d\\+\\//g), 'message: You should be using the following expression <code>/\\d+/g</code> to find the numbers in the <code>testString</code>.');"
"assert(editor.getValue().match(/\\/\\\\d\\+\\//g), 'message: Use the <code>/\\d+/g</code> regular expression to find the numbers in <code>testString</code>.');",
"assert(test === 2, 'message: Your regular expression should find two numbers in <code>testString</code>.');"
],
"challengeSeed": [
"var test = (function() {",
"",
" var testString = \"There are 3 cats but 4 dogs.\";",
"",
" // Only change code below this line.",
@@ -1127,8 +1141,9 @@
" var expression = /.+/g;",
"",
" // Only change code above this line.",
" // We use this function to show you the value of your variable in your output box.",
"",
" return testString.match(expression).length;",
"",
"})();(function(){return test;})();"
],
"type": "waypoint",
@@ -1138,15 +1153,15 @@
"id": "cf1111c1c12feddfaeb8bdef",
"title": "Find Whitespace with Regular Expressions",
"description": [
"We can also use selectors like <code>\\s</code> to find whitespace in a string.",
"The whitespace characters are <code>\" \"</code> (space), <code>\\r</code> (carriage return), <code>\\n</code> (newline), <code>\\t</code> (tab), and <code>\\f</code> (form feed).",
"It is used like this:",
"We can also use regular expression selectors like <code>\\s</code> to find whitespace in a string.",
"The whitespace characters are <code>\" \"</code> (space), <code>\\r</code> (the carriage return), <code>\\n</code> (newline), <code>\\t</code> (tab), and <code>\\f</code> (the form feed).",
"The whitespace regular expression looks like this:",
"<code>/\\s+/g</code>",
"Select all the whitespace characters in the sentence string."
"Use it to select all the whitespace characters in the sentence string."
],
"tests": [
"assert(test === 7, 'message: Your RegEx should have found seven spaces in the <code>testString</code>.');",
"assert(editor.getValue().match(/\\/\\\\s\\+\\//g), 'message: You should be using the following expression <code>/\\s+/g</code> to find the spaces in the <code>testString</code>.');"
"assert(editor.getValue().match(/\\/\\\\s\\+\\//g), 'message: Use the <code>/\\s+/g</code> regular expression to find the spaces in <code>testString</code>.');",
"assert(test === 7, 'message: Your regular expression should find seven spaces in <code>testString</code>.');"
],
"challengeSeed": [
"var test = (function(){",
@@ -1157,8 +1172,9 @@
" var expression = /.+/g;",
"",
" // Only change code above this line.",
" // We use this function to show you the value of your variable in your output box.",
"",
" return testString.match(expression).length;",
"",
"})();(function(){return test;})();"
],
"type": "waypoint",
@@ -1168,12 +1184,13 @@
"id": "cf1111c1c13feddfaeb3bdef",
"title": "Invert Regular Expression Matches with JavaScript",
"description": [
"Use <code>/\\S/g</code> to match everything that isn't a space in the string.",
"You can invert any match by using the uppercase version of the selector <code>\\s</code> versus <code>\\S</code> for example."
"You can invert any match by using the uppercase version of the regular expression selector.",
"For example, <code>\\s</code> will match any whitespace, and <code>\\S</code> will match anything that isn't whitespace.",
"Use <code>/\\S/g</code> to count the number of non-whitespace characters in <code>testString</code>.",
],
"tests": [
"assert(test === 49, 'message: Your RegEx should have found forty nine non-space characters in the <code>testString</code>.');",
"assert(editor.getValue().match(/\\/\\\\S\\/g/g), 'message: You should be using the following expression <code>/\\S/g</code> to find non-space characters in the <code>testString</code>.');"
"assert(editor.getValue().match(/\\/\\\\S\\/g/g), 'message: Use the <code>/\\S/g</code> regular expression to find non-space characters in <code>testString</code>.');",
"assert(test === 49, 'message: Your regular expression should find forty nine non-space characters in the <code>testString</code>.');"
],
"challengeSeed": [
"var test = (function(){",
@@ -1184,7 +1201,7 @@
" var expression = /./g;",
"",
" // Only change code above this line.",
" // We use this function to show you the value of your variable in your output box.",
"",
" return testString.match(expression).length;",
"})();(function(){return test;})();"
],