3.3 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244bd Passing Values to Functions with Arguments 1 将值传递给带参数的函数

Description

参数是变量,它们作为调用函数时要输入到函数的值的占位符。定义函数时,通常将其与一个或多个参数一起定义。调用函数时输入(或“传递” )的实际值称为参数 。这是一个带有两个参数的函数, param1param2
function testFunparam1param2{
console.logparam1param2;
}
然后我们可以调用testFun testFun("Hello", "World");我们通过了两个论点, "Hello""World" 。在函数内部, param1将等于“Hello” param2将等于“World”。请注意您可以使用不同的参数再次调用testFun ,并且参数将采用新参数的值。

Instructions

  1. 创建一个名为functionWithArgs ,该函数接受两个参数并将其总和输出到开发控制台。
  2. 使用两个数字作为参数调用该函数。

Tests

tests:
  - text: <code>functionWithArgs</code>应该是一个函数
    testString: 'assert(typeof functionWithArgs === "function", "<code>functionWithArgs</code> should be a function");'
  - text: '<code>functionWithArgs(1,2)</code>应该输出<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>应该输出<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: 定义后,使用两个数字调用<code>functionWithArgs</code> 。
    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

console.info('after the test');

Solution

// solution required