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

3.0 KiB

id title challengeType videoUrl localeTitle
cf1111c1c12feddfaeb2bdef Generate Random Whole Numbers within a Range 1 Gerar números inteiros aleatórios dentro de um intervalo

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 min e um número máximo max . Aqui está a fórmula que vamos usar. Tome um momento para lê-lo e tente entender o que este código está fazendo: Math.floor(Math.random() * (max - min + 1)) + min

Instructions

Crie uma função chamada randomRange que randomRange um intervalo myMin e myMax e retorne um número aleatório que seja maior ou igual a myMin e seja menor ou igual a myMax , inclusive.

Tests

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.");'

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