Randell Dawson e0e6334628
fix(curriculum): Consolidated comments for JavaScript Algorithms and Data Structures challenges - part 3 of 4 (#38264)
* fix: remove example code from challenge seed

* fix: remove declaration from solution

* fix: added sum variable back in

* fix: reverted description back to original version

* fix: added examples to description section

* fix: added complete sentence

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: corrected typo

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

* fix: reverted to original desc with formatted code

* fix: removed unnecessary code example from description section

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: failiing test on iterate through array with for loop

* fix: changed to Only change this line

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: Manish Giri <manish.giri.me@gmail.com>
Co-authored-by: moT01 <tmondloch01@gmail.com>
2020-03-25 16:07:13 +01:00

2.6 KiB

id, title, challengeType, videoUrl, forumTopicId
id title challengeType videoUrl forumTopicId
cf1111c1c12feddfaeb2bdef Generate Random Whole Numbers within a Range 1 https://scrimba.com/c/cm83yu6 18187

Description

Instead of generating a random number between zero and a given number like we did before, we can generate a random number that falls within a range of two specific numbers. To do this, we'll define a minimum number min and a maximum number max. Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing: Math.floor(Math.random() * (max - min + 1)) + min

Instructions

Create a function called randomRange that takes a range myMin and myMax and returns a random number that's greater than or equal to myMin, and is less than or equal to myMax, inclusive.

Tests

tests:
  - text: The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.
    testString: assert(calcMin === 5);
  - text: The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.
    testString: assert(calcMax === 15);
  - text: The random number generated by <code>randomRange</code> should be an integer, not a decimal.
    testString: assert(randomRange(0,1) % 1 === 0 );
  - text: <code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.
    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;}})());

Challenge Seed

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

After Test

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

Solution

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