* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.8 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244bd | Passing Values to Functions with Arguments | 1 | https://scrimba.com/c/cy8rahW | 18254 | passing-values-to-functions-with-arguments |
--description--
Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or "passed") into a function when it is called are known as arguments.
Here is a function with two parameters, param1
and param2
:
function testFun(param1, param2) {
console.log(param1, param2);
}
Then we can call 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.
--hints--
functionWithArgs
should be a function.
assert(typeof functionWithArgs === 'function');
functionWithArgs(1,2)
should output 3
.
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(1, 2);
uncapture();
}
assert(logOutput == 3);
functionWithArgs(7,9)
should output 16
.
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(7, 9);
uncapture();
}
assert(logOutput == 16);
You should call functionWithArgs
with two numbers after you define it.
assert(
/functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(
code.replace(/\s/g, '')
)
);
--seed--
--before-user-code--
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-user-code--
uncapture();
if (typeof functionWithArgs !== "function") {
(function() { return "functionWithArgs is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
--seed-contents--
--solutions--
function functionWithArgs(a, b) {
console.log(a + b);
}
functionWithArgs(10, 5);