More Challenges

This commit is contained in:
SaintPeter 2015-12-21 13:50:45 -08:00
parent 9c4094582f
commit 882a1c0818

View File

@ -1709,17 +1709,16 @@
"description": [
"In JavaScript, we can divide up our code into reusable parts called <dfn>functions</dfn>.",
"Here's an example of a function:",
"<code>function functionName() {</code>",
"<code>&nbsp;&nbsp;console.log(\"Hello World\");</code>",
"<code>}</code>",
"You can call or <dfn>invoke</dfn> this function by using its name followed by parenthese, like this:",
"<blockquote>function functionName() {<br /> console.log(\"Hello World\");<br />}</blockquote>",
"You can call or <dfn>invoke</dfn> this function by using its name followed by parentheses, like this:",
"<code>functionName();</code>",
"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.",
"<h4>Instructions</h4>Create a function called <code>myFunction</code> which prints \"Hi World\" to the dev console. Call that function."
],
"tests": [
"console.log(\"1 '\" + logOutput + \"'\", \"op: \" + /Hi World/.test(logOutput)); assert(\"Hi World\" === logOutput, 'message: <code>myFunction</code> should output \"Hi World\"');",
"assert(typeof myFunction === 'function', 'message: <code>myFunction</code> should be a function');"
"assert(typeof myFunction === 'function', 'message: <code>myFunction</code> should be a function');",
"assert(\"Hi World\" === logOutput, 'message: <code>myFunction</code> should output \"Hi World\" to the dev console');",
"assert(/^\\s*myFunction\\(\\)\\s*;/m.test(editor.getValue()), 'message: Call <code>myFunction</code> after you define it');"
],
"challengeSeed": [
"// Example",
@ -1727,6 +1726,8 @@
" console.log(\"Heyya, World\");",
"}",
"",
"ourFunction();",
"",
"// Only change code below this line",
"",
""
@ -1757,7 +1758,8 @@
"solutions": [
"function myFunction() {",
" console.log(\"Hi World\");",
"}"
"}",
"myFunction();"
],
"type": "waypoint",
"challengeType": "1"
@ -1767,17 +1769,34 @@
"title": "Passing Values to Functions with Arguments",
"description": [
"Functions can take input in the form of <dfn>parameters</dfn>. <code>Parameters</code> are variables that take on the value of the <dfn>arguments</dfn> which are passed in to the function. Here is a function with two parameters, <code>param1</code> and <code>param2</code>:",
"<code>function testFunct(param1, param2) {</code>",
"<code>&nbsp;&nbsp;console.log(param1, param2);</code>",
"<code>}</code>",
"Then we can call <code>testFunction</code>:",
"<code>testFunction(\"Hello\", \"World\");</code>",
"We have <dfn>passed</dfn> two arguments, \"Hello\" and \"World\". Inside the function, <code>param1</code> will equal \"Hello\" and <code>param2</code> will equal \"World\". Note that you could call <code>testFunction</code> again with different arguments and the parameters would take on the value of the new arguments.",
"<h4>Instructions</h4>Create a function called <code>myFunction</code> that accepts two arguements and outputs their sum to <code>console.log</code>"
"<blockquote>function testFun(param1, param2) {<br /> console.log(param1, param2);<br />}</blockquote>",
"Then we can call <code>testFun</code>:",
"<code>testFun(\"Hello\", \"World\");</code>",
"We have <dfn>passed</dfn> two arguments, \"Hello\" and \"World\". Inside the function, <code>param1</code> will equal \"Hello\" and <code>param2</code> will equal \"World\". Note that you could call <code>testFun</code> again with different arguments and the parameters would take on the value of the new arguments.",
"<h4>Instructions</h4>Create a function called <code>myFunction</code> that accepts two arguments and outputs their sum to the dev console. Call your function."
],
"releasedOn": "11/27/2015",
"tests": [
"assert((function(){if(typeof(f) !== \"undefined\" && f === a + b){return true;}else{return false;}})(), 'message: Your function should ouput the value of <code>a + b</code>.');"
"assert(typeof myFunction === 'function', 'message: <code>myFunction</code> should be a function');",
"capture(); myFunction(1,2); uncapture(); assert(logOutput == 3, 'message: <code>myFunction(1,2)</code> should output <code>3</code>');",
"capture(); myFunction(7,9); uncapture(); assert(logOutput == 16, 'message: <code>myFunction(7,9)</code> should output <code>16</code>');",
"assert(/^\\s*myFunction\\(\\s*\\d+\\s*,\\s*\\d+\\s*\\)\\s*;/m.test(editor.getValue()), 'message: Call <code>myFunction</code> after you define it');"
],
"head": [
"var logOutput = \"\";",
"var oldLog = console.log;",
"function capture() {",
" console.log = function (message) {",
" logOutput = message + ''.trim();",
" oldLog.apply(console, arguments);",
" };",
"}",
"",
"function uncapture() {",
" console.log = oldLog;",
"}",
"",
"capture();"
],
"challengeSeed": [
"// Example",
@ -1788,11 +1807,16 @@
"",
"// Only change code below this line.",
"",
"",
""
],
"tail": [
""
"uncapture();",
"",
"if (typeof myFunction !== \"function\") { ",
" (function() { return \"myFunction is not defined\"; })();",
"} else {",
" (function() { return logOutput || \"console.log never called\"; })();",
"}"
],
"solutions": [
""
@ -1809,19 +1833,60 @@
"id": "56533eb9ac21ba0edf2244be",
"title": "Global Scope and Functions",
"description": [
"In Javascript, <dfn>scope</dfn> refers to the visibility of variables."
"In Javascript, <dfn>scope</dfn> refers to the visibility of variables. Variables which are defined outside of a function block have <dfn>Global</dfn> scope. This means the can be seen everywhere in your Javascript code. ",
"Variables which are used without the <code>var</code> keyword are automatically created in the <code>global</code> scope. This can create unintended concequences elsewhere in your code or when running a function again. You should always declare your variables with <code>var</code>.",
"<h4>Instructions</h4>Declare a <code>global</code> variable <code>myGlobal</code> outside of any function. Initialize it to have a value of <code>10</code> ",
"Inside function <code>fun1</code>, assign <code>5</code> to <code>oopsGlobal</code> <strong><em>without</em></strong> using the <code>var</code> keyword."
],
"releasedOn": "11/27/2015",
"tests": [
"assert(1===1, 'message: message here');"
"assert(typeof myGlobal != \"undefined\", 'message: <code>myGlobal</code> should be defined');",
"assert(myGlobal === 10, 'message: <code>myGlobal</code> should have a value of <code>10</code>');",
"assert(/var\\s+myGlobal/.test(editor.getValue()), 'message: <code>myGlobal</code> should be declared using the <code>var</code> keyword');",
"assert(typeof oopsGlobal != \"undefined\" && oopsGlobal === 5, 'message: <code>oopsGlobal</code> should have a value of <code>5</code>');",
"assert(!/var\\s+oopsGlobal/.test(editor.getValue()), 'message: Do not decalre <code>oopsGlobal</code> using the <code>var</code> keyword');"
],
"head": [
"var logOutput = \"\";",
"var oldLog = console.log;",
"function capture() {",
" console.log = function (message) {",
" logOutput = message;",
" oldLog.apply(console, arguments);",
" };",
"}",
"",
"function uncapture() {",
" console.log = oldLog;",
"}",
"",
"capture();"
],
"challengeSeed": [
"// Declare your variable here",
"",
"",
""
"function fun1() {",
" // Assign 5 to oopsGlobal Here",
"}",
"",
"// Only change code above this line",
"function fun2() {",
" var output = \"\";",
" if(typeof myGlobal != \"undefined\") {",
" output += \"myGlobal: \" + myGlobal;",
" }",
" if(typeof oopsGlobal != \"undefined\") {",
" output += \" oopsGlobal: \" + oopsGlobal;",
" }",
" console.log(output);",
"}"
],
"tail": [
""
"fun1();",
"fun2();",
"uncapture();",
"(function() { return logOutput || \"console.log never called\"; })();"
],
"solutions": [
""
@ -1838,10 +1903,11 @@
"id": "56533eb9ac21ba0edf2244bf",
"title": "Local Scope and Functions",
"description": [
"In Javascript, <code>Scope</code> is the test of variables, object, and functions you have access to. Variables which are defined within a function and parameters are <dfn>local</dfn>, which means they are only visible within that function. ",
"Here is a function <code>test</code> with a local variable called <code>loc</code>.",
"<blockquote>function test() {<br /> var loc = \"foo\";<br /> console.log(local1); // \"foo\"<br />}<br />test();<br />console.log(loc); // \"undefined\"</blockquote>",
"<code>loc</code> is not defined outside of the function."
"Variables which are declared within a function, as well as function parameters are <dfn>local</dfn>. Thos means they are only visible within that function. ",
"Here is a function <code>myTest</code> with a local variable called <code>loc</code>.",
"<blockquote>function myTest() {<br /> var local1 = \"foo\";<br /> console.log(local1);<br />}<br />myTest(); // \"foo\"<br />console.log(local1); // \"undefined\"</blockquote>",
"<code>local1</code> is not defined outside of the function.",
"<h4>Instructions</h4>Declare a local variable <code>myVar</code> inside <code>myFunction</code>"
],
"releasedOn": "11/27/2015",
"tests": [