freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../basic-javascript/generate-random-whole-numbe...

90 lines
3.1 KiB
Markdown

---
id: cf1111c1c12feddfaeb2bdef
title: Generate Random Whole Numbers within a Range
localeTitle: Generar números enteros al azar dentro de un rango
challengeType: 1
---
## Description
<section id='description'>
En lugar de generar un número aleatorio entre cero y un número dado como hicimos antes, podemos generar un número aleatorio que se encuentre dentro de un rango de dos números específicos.
Para hacer esto, definiremos un número mínimo <code>min</code> y un máximo número <code>max</code> .
Aquí está la fórmula que usaremos. Tómese un momento para leerlo e intente entender lo que hace este código:
<code>Math.floor(Math.random() * (max - min + 1)) + min</code>
</section>
## Instructions
<section id='instructions'>
Cree una función llamada <code>randomRange</code> que tome un rango <code>myMin</code> y <code>myMax</code> y devuelva un número aleatorio que sea mayor o igual que <code>myMin</code> , y que sea menor o igual que <code>myMax</code> , inclusive.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: &#39;El número aleatorio más bajo que puede ser generado por <code>randomRange</code> debería ser igual a tu número mínimo, <code>myMin</code> &#39;.
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: &quot;El número aleatorio más alto que puede ser generado por <code>randomRange</code> debería ser igual a tu número máximo, <code>myMax</code> &quot;.
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: &#39;El número aleatorio generado por <code>randomRange</code> debe ser un número entero, no un decimal&#39;.
testString: 'assert(randomRange(0,1) % 1 === 0 , "The random number generated by <code>randomRange</code> should be an integer, not a decimal.");'
- text: &#39; <code>randomRange</code> debe usar <code>myMax</code> y <code>myMin</code> , y devolver un número aleatorio en tu rango&#39;.
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
function randomRange(myMin, myMax) {
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
```
</section>