81 lines
3.0 KiB
Markdown
81 lines
3.0 KiB
Markdown
|
---
|
||
|
id: cf1111c1c12feddfaeb2bdef
|
||
|
title: Generate Random Whole Numbers within a Range
|
||
|
challengeType: 1
|
||
|
videoUrl: ''
|
||
|
localeTitle: Gerar números inteiros aleatórios dentro de um intervalo
|
||
|
---
|
||
|
|
||
|
## Description
|
||
|
<section id="description"> Em vez de gerar um número aleatório entre zero e um dado número, como fizemos antes, podemos gerar um número aleatório que esteja dentro de um intervalo de dois números específicos. Para fazer isso, definiremos um número mínimo <code>min</code> e um número máximo <code>max</code> . Aqui está a fórmula que vamos usar. Tome um momento para lê-lo e tente entender o que este código está fazendo: <code>Math.floor(Math.random() * (max - min + 1)) + min</code> </section>
|
||
|
|
||
|
## Instructions
|
||
|
<section id="instructions"> Crie uma função chamada <code>randomRange</code> que <code>randomRange</code> um intervalo <code>myMin</code> e <code>myMax</code> e retorne um número aleatório que seja maior ou igual a <code>myMin</code> e seja menor ou igual a <code>myMax</code> , inclusive. </section>
|
||
|
|
||
|
## Tests
|
||
|
<section id='tests'>
|
||
|
|
||
|
```yml
|
||
|
tests:
|
||
|
- text: 'O menor número aleatório que pode ser gerado por <code>randomRange</code> deve ser igual ao seu número mínimo, <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: 'O maior número aleatório que pode ser gerado por <code>randomRange</code> deve ser igual ao seu número máximo, <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: 'O número aleatório gerado por <code>randomRange</code> deve ser um inteiro, não um decimal.'
|
||
|
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> deve usar <code>myMax</code> e <code>myMin</code> e retornar um número aleatório no seu intervalo.
|
||
|
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.");'
|
||
|
|
||
|
```
|
||
|
|
||
|
</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
|
||
|
console.info('after the test');
|
||
|
```
|
||
|
|
||
|
</div>
|
||
|
|
||
|
</section>
|
||
|
|
||
|
## Solution
|
||
|
<section id='solution'>
|
||
|
|
||
|
```js
|
||
|
// solution required
|
||
|
```
|
||
|
</section>
|