freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../debugging/catch-arguments-passed-in-t...

1.8 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b85367417b2b2512b3a Catch Arguments Passed in the Wrong Order When Calling a Function 1 调用函数时捕获以错误顺序传递的参数

Description

继续讨论调用函数,需要注意的下一个错误是函数的参数是以错误的顺序提供的。如果参数是不同的类型,例如期望数组和整数的函数,则可能会引发运行时错误。如果参数是相同的类型(例如,所有整数),那么代码的逻辑将没有意义。确保以正确的顺序提供所有必需的参数以避免这些问题。

Instructions

函数raiseToPower将基数提升为指数。不幸的是,它没有被正确调用 - 修复代码,因此power值是预期的8。

Tests

tests:
  - text: 你的代码应该固定可变<code>power</code>因此它等于2提升到3功率而不是3增加到2功率。
    testString: 'assert(power == 8, "Your code should fix the variable <code>power</code> so it equals 2 raised to the 3rd power, not 3 raised to the 2nd power.");'
  - text: 您的代码应使用<code>raiseToPower</code>函数调用的正确参数顺序。
    testString: 'assert(code.match(/raiseToPower\(\s*?base\s*?,\s*?exp\s*?\);/g), "Your code should use the correct order of the arguments for the <code>raiseToPower</code> function call.");'

Challenge Seed

function raiseToPower(b, e) {
  return Math.pow(b, e);
}

let base = 2;
let exp = 3;
let power = raiseToPower(exp, base);
console.log(power);

Solution

// solution required