mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-02 03:03:23 -05:00
1.7 KiB
1.7 KiB
id, title, challengeType, videoUrl, forumTopicId
| id | title | challengeType | videoUrl | forumTopicId |
|---|---|---|---|---|
| 56533eb9ac21ba0edf2244bd | 将值传递给带有参数的函数 | 1 | https://scrimba.com/c/cy8rahW | 18254 |
--description--
函数的参数parameters在函数中充当占位符(也叫形参)的作用,参数可以为一个或多个。调用一个函数时所传入的参数为实参,实参决定着形参真正的值。简单理解:形参即形式、实参即内容。
这是带有两个参数的函数,param1和param2:
function testFun(param1, param2) {
console.log(param1, param2);
}
接着我们调用testFun: testFun("Hello", "World"); 我们传递了两个参数,"Hello"和"World"。在函数内部,param1等于“Hello”,param2等于“World”。请注意,testFun函数可以多次调用,每次调用时传递的参数会决定形参的实际值。
--instructions--
- 创建一个名为
functionWithArgs的函数,它可以接收两个参数,计算参数的和,将结果输出到控制台。 - 调用这个函数。
--hints--
functionWithArgs应该是一个函数。
assert(typeof functionWithArgs === 'function');
functionWithArgs(1,2)应该输出3。
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(1, 2);
uncapture();
}
assert(logOutput == 3);
functionWithArgs(7,9)应该输出16。
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(7, 9);
uncapture();
}
assert(logOutput == 16);
在你定义functionWithArgs之后记得调用它。
assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*;?/m.test(code));