2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: cf1111c1c12feddfaeb2bdef
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cm83yu6'
|
|
|
|
|
forumTopicId: 18187
|
|
|
|
|
localeTitle: 生成某个范围内的随机整数
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
我们之前生成的随机数是在0到某个数之间,现在我们要生成的随机数是在两个指定的数之间。
|
|
|
|
|
我们需要定义一个最小值和一个最大值。
|
|
|
|
|
下面是我们将要使用的方法,仔细看看并尝试理解这行代码到底在干嘛:
|
|
|
|
|
<code>Math.floor(Math.random() * (max - min + 1)) + min</code>
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
创建一个叫<code>randomRange</code>的函数,参数为 myMin 和 myMax,返回一个在<code>myMin</code>(包括 myMin)和<code>myMax</code>(包括 myMax)之间的随机数。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>randomRange</code>返回的随机数应该大于或等于<code>myMin</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(calcMin === 5);
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>randomRange</code>返回的随机数应该小于或等于<code>myMax</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(calcMax === 15);
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>randomRange</code>应该返回一个随机整数, 而不是小数。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(randomRange(0,1) % 1 === 0 );
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>randomRange</code>应该使用<code>myMax</code>和<code>myMin</code>, 并且返回两者之间的随机数。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
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;}})());
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// 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);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
var calcMin = 100;
|
|
|
|
|
var calcMax = -100;
|
|
|
|
|
for(var i = 0; i < 100; i++) {
|
|
|
|
|
var result = randomRange(5,15);
|
|
|
|
|
calcMin = Math.min(calcMin, result);
|
|
|
|
|
calcMax = Math.max(calcMax, result);
|
|
|
|
|
}
|
|
|
|
|
(function(){
|
|
|
|
|
if(typeof myRandom === 'number') {
|
|
|
|
|
return "myRandom = " + myRandom;
|
|
|
|
|
} else {
|
|
|
|
|
return "myRandom undefined";
|
|
|
|
|
}
|
|
|
|
|
})()
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
function randomRange(myMin, myMax) {
|
|
|
|
|
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
|
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|