2020-10-06 23:10:08 +05:30

1.7 KiB
Raw Blame History

id, challengeType, videoUrl, forumTopicId, title
id challengeType videoUrl forumTopicId title
cf1111c1c11feddfaeb9bdef 1 https://scrimba.com/c/cyWJJs3 18185 使用 JavaScript 生成随机分数

Description

随机数非常适合用来创建随机行为。 Math.random()用来生成一个在0(包括 01(不包括 1之间的随机小数因此Math.random()可能返回 0 但绝不会返回 1。 提示
使用赋值运算符存储值这一节讲过,所有函数调用将在return执行之前解析,因此我们可以返回Math.random()函数的值。

Instructions

更改randomFraction使其返回一个随机数而不是0

Tests

tests:
  - text: <code>randomFraction</code>应该返回一个随机数。
    testString: assert(typeof randomFraction() === "number");
  - text: <code>randomFraction</code>应该返回一个小数。
    testString: assert((randomFraction()+''). match(/\./g));
  - text: 需要使用<code>Math.random</code>生成随机的小数。
    testString: assert(code.match(/Math\.random/g).length >= 0);

Challenge Seed

function randomFraction() {

  // Only change code below this line.

  return 0;

  // Only change code above this line.
}

After Test

(function(){return randomFraction();})();

Solution

function randomFraction() {
  return Math.random();
}