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.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
cf1111c1c12feddfaeb1bdef 使用 JavaScript 生成随机整数 1 https://scrimba.com/c/cRn6bfr 18186 generate-random-whole-numbers-with-javascript

--description--

生成随机小数很棒,但随机数更有用的地方在于生成随机整数。

  1. Math.random()生成一个随机小数。
  2. 把这个随机小数乘以20
  3. Math.floor()向下取整 获得它最近的整数。

记住Math.random()永远不会返回1。同时因为我们是在用Math.floor()向下取整,所以最终我们获得的结果不可能有20。这确保了我们获得了一个在0到19之间的整数。

把操作连缀起来,代码类似于下面:

Math.floor(Math.random() * 20);

我们先调用Math.random()把它的结果乘以20然后把上一步的结果传给Math.floor(),最终通过向下取整获得最近的整数。

--instructions--

生成一个09之间的随机整数。

--hints--

myFunction的结果应该是一个整数。

assert(
  typeof randomWholeNum() === 'number' &&
    (function () {
      var r = randomWholeNum();
      return Math.floor(r) === r;
    })()
);

需要使用Math.random生成随机数字。

assert(code.match(/Math.random/g).length > 1);

你应该将Math.random的结果乘以 10 来生成 0 到 9 之间的随机数。

assert(
  code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) ||
    code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g)
);

你需要使用Math.floor移除数字中的小数部分。

assert(code.match(/Math.floor/g).length > 1);

--seed--

--after-user-code--

(function(){return randomWholeNum();})();

--seed-contents--

function randomWholeNum() {

  // Only change code below this line

  return Math.random();
}

--solutions--

function randomWholeNum() {
  return Math.floor(Math.random() * 10);
}