2.8 KiB

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
cf1111c1c12feddfaeb2bdef Generate Random Whole Numbers within a Range 1 生成范围内的随机整数

Description

我们可以生成一个落在两个特定数字范围内的随机数,而不是像我们之前那样在零和给定数字之间生成一个随机数。为此,我们将定义最小数量min和最大数量max 。这是我们将使用的公式。花一点时间阅读它并尝试理解这段代码在做什么: Math.floor(Math.random() * (max - min + 1)) + min

Instructions

创建一个名为randomRange的函数,它接受一个范围myMinmyMax并返回一个大于或等于myMin的随机数,并且小于或等于myMax (包括myMax )。

Tests

tests:
  - text: <code>randomRange</code>可以生成的最低随机数应该等于你的最小数量<code>myMin</code> 。
    testString: 'assert(calcMin === 5, "The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.");'
  - text: <code>randomRange</code>可以生成的最高随机数应该等于最大数量<code>myMax</code> 。
    testString: 'assert(calcMax === 15, "The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.");'
  - text: <code>randomRange</code>生成的随机数应该是整数,而不是小数。
    testString: 'assert(randomRange(0,1) % 1 === 0 , "The random number generated by <code>randomRange</code> should be an integer, not a decimal.");'
  - text: <code>randomRange</code>应该同时使用<code>myMax</code>和<code>myMin</code> ,并在你的范围内返回一个随机数。
    testString: 'assert((function(){if(code.match(/myMax/g).length > 1 && code.match(/myMin/g).length > 2 && code.match(/Math.floor/g) && code.match(/Math.random/g)){return true;}else{return false;}})(), "<code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.");'

Challenge Seed

// Example
function ourRandomRange(ourMin, ourMax) {

  return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
}

ourRandomRange(1, 9);

// Only change code below this line.

function randomRange(myMin, myMax) {

  return 0; // Change this line

}

// Change these values to test your function
var myRandom = randomRange(5, 15);

After Test

console.info('after the test');

Solution

// solution required