Files
freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range.md
camperbot 7c3cbbbfd5 chore(i18n,learn): processed translations (#41416)
* chore(i8n,learn): processed translations

* Update curriculum/challenges/chinese/01-responsive-web-design/applied-visual-design/use-the-u-tag-to-underline-text.md

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
2021-03-08 06:28:46 -08:00

2.3 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
cf1111c1c12feddfaeb2bdef Genera números enteros aleatorios dentro de un rango 1 https://scrimba.com/c/cm83yu6 18187 generate-random-whole-numbers-within-a-range

--description--

En lugar de generar un número entero aleatorio entre cero y un número dado como lo hicimos anteriormente, podemos generar un número entero aleatorio que se encuentre dentro de un rango de dos números específicos.

Para ello, definiremos un número mínimo min y un número máximo max.

Esta es la fórmula que utilizaremos. Tómate un momento para leerla e intenta entender lo que este código está haciendo:

Math.floor(Math.random() * (max - min + 1)) + min

--instructions--

Crea una función llamada randomRange que tenga un rango myMin y myMax y devuelva un número entero aleatorio mayor o igual a myMin, y es menor o igual a myMax, inclusivo.

--hints--

El número aleatorio más bajo que puede ser generado por randomRange debe ser igual a tu número mínimo, myMin.

assert(calcMin === 5);

El número aleatorio más alto que puede ser generado por randomRange debe ser igual a tu número máximo, myMax.

assert(calcMax === 15);

El número aleatorio generado por randomRange debe ser un entero, no un decimal.

assert(randomRange(0, 1) % 1 === 0);

randomRange debe usar myMax y myMin, y devolver un número aleatorio en tu rango.

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;
    }
  })()
);

--seed--

--after-user-code--

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";
  }
})()

--seed-contents--

function randomRange(myMin, myMax) {
  // Only change code below this line
  return 0;
  // Only change code above this line
}

--solutions--

function randomRange(myMin, myMax) {
  return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}