freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/generate-random-fractions-w...

2.0 KiB

id title challengeType videoUrl localeTitle
cf1111c1c11feddfaeb9bdef Generate Random Fractions with JavaScript 1 使用JavaScript生成随机分数

Description

随机数对于创建随机行为很有用。 JavaScript有一个Math.random()函数,它生成一个介于0 (含)和不高达1 (独占)之间的随机十进制数。因此Math.random()可以返回0但永远不会返回1 Note
与使用Equal运算符存储值一样,所有函数调用将在return执行之前解析,因此我们可以return Math.random()函数的值。

Instructions

更改randomFraction以返回随机数而不是返回0

Tests

tests:
  - text: <code>randomFraction</code>应该返回一个随机数。
    testString: 'assert(typeof randomFraction() === "number", "<code>randomFraction</code> should return a random number.");'
  - text: <code>randomFraction</code>返回的<code>randomFraction</code>应该是小数。
    testString: 'assert((randomFraction()+""). match(/\./g), "The number returned by <code>randomFraction</code> should be a decimal.");'
  - text: 您应该使用<code>Math.random</code>来生成随机十进制数。
    testString: 'assert(code.match(/Math\.random/g).length >= 0, "You should be using <code>Math.random</code> to generate the random decimal number.");'

Challenge Seed

function randomFraction() {

  // Only change code below this line.

  return 0;

  // Only change code above this line.
}

After Test

console.info('after the test');

Solution

// solution required