* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
3.5 KiB
3.5 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
56533eb9ac21ba0edf2244bd | Passing Values to Functions with Arguments | 1 |
Description
param1
and param2
:
function testFun(param1, param2) {Then we can call
console.log(param1, param2);
}
testFun
:
testFun("Hello", "World");
We have passed two arguments, "Hello"
and "World"
. Inside the function, param1
will equal "Hello" and param2
will equal "World". Note that you could call testFun
again with different arguments and the parameters would take on the value of the new arguments.
Instructions
- Create a function called
functionWithArgs
that accepts two arguments and outputs their sum to the dev console. - Call the function with two numbers as arguments.
Tests
tests:
- text: <code>functionWithArgs</code> should be a function
testString: assert(typeof functionWithArgs === 'function', '<code>functionWithArgs</code> should be a function');
- text: <code>functionWithArgs(1,2)</code> should output <code>3</code>
testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(1,2); uncapture(); } assert(logOutput == 3, '<code>functionWithArgs(1,2)</code> should output <code>3</code>');
- text: <code>functionWithArgs(7,9)</code> should output <code>16</code>
testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(7,9); uncapture(); } assert(logOutput == 16, '<code>functionWithArgs(7,9)</code> should output <code>16</code>');
- text: Call <code>functionWithArgs</code> with two numbers after you define it.
testString: assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*;/m.test(code), 'Call <code>functionWithArgs</code> with two numbers after you define it.');
Challenge Seed
// Example
function ourFunctionWithArgs(a, b) {
console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5
// Only change code below this line.
Before Test
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
if(message) logOutput = JSON.stringify(message).trim();
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
capture();
After Test
uncapture();
if (typeof functionWithArgs !== "function") {
(function() { return "functionWithArgs is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
Solution
function functionWithArgs(a, b) {
console.log(a + b);
}
functionWithArgs(10, 5);