Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

2.2 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cf 用函数编写可重用代码 1 https://scrimba.com/c/cL6dqfy 18378 write-reusable-javascript-with-functions

--description--

在 JavaScript 中,我们可以把代码的重复部分抽取出来,放到一个函数中。

举个例子:

function functionName() {
  console.log("Hello World");
}

你可以通过函数名functionName加上后面的小括号来调用这个函数,就像这样: functionName(); 每次调用函数时,它都会在控制台上打印消息"Hello World"。每次调用函数时,大括号之间的所有代码都将被执行。

--instructions--

  1. 先创建一个名为reusableFunction的函数,这个函数可以打印"Hi World"到控制台上。
  2. 然后调用这个函数。

--hints--

reusableFunction应该是一个函数。

assert(typeof reusableFunction === 'function');

reusableFunction应该在控制台中输出 "Hi World"。

assert(hiWorldWasLogged);

在你定义reusableFunction之后记得调用它。

assert(/^\s*reusableFunction\(\)\s*/m.test(code));

--seed--

--before-user-code--

var logOutput = "";
var originalConsole = console;
var nativeLog = console.log;
var hiWorldWasLogged = false;
function capture() {
    console.log = function (message) {
        if(message === 'Hi World')  hiWorldWasLogged = true;
        if(message && message.trim) logOutput = message.trim();
        if(nativeLog.apply) {
          nativeLog.apply(originalConsole, arguments);
        } else {
          var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
          nativeLog(nativeMsg);
        }
    };
}

function uncapture() {
  console.log = nativeLog;
}

capture();

--after-user-code--

uncapture();

if (typeof reusableFunction !== "function") { 
  (function() { return "reusableFunction is not defined"; })();
} else {
  (function() { return logOutput || "console.log never called"; })();
}

--seed-contents--

--solutions--

function reusableFunction() {
  console.log("Hi World");
}
reusableFunction();