* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.0 KiB
2.0 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
cf1111c1c12feddfaeb2bdef | 生成某个范围内的随机整数 | 1 | https://scrimba.com/c/cm83yu6 | 18187 | generate-random-whole-numbers-within-a-range |
--description--
我们之前生成的随机数是在0到某个数之间,现在我们要生成的随机数是在两个指定的数之间。
我们需要定义一个最小值和一个最大值。
下面是我们将要使用的方法,仔细看看并尝试理解这行代码到底在干嘛:
Math.floor(Math.random() * (max - min + 1)) + min
--instructions--
创建一个叫randomRange
的函数,参数为 myMin 和 myMax,返回一个在myMin
(包括 myMin)和myMax
(包括 myMax)之间的随机数。
--hints--
randomRange
返回的随机数应该大于或等于myMin
。
assert(calcMin === 5);
randomRange
返回的随机数应该小于或等于myMax
。
assert(calcMax === 15);
randomRange
应该返回一个随机整数, 而不是小数。
assert(randomRange(0, 1) % 1 === 0);
randomRange
应该使用myMax
和myMin
, 并且返回两者之间的随机数。
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;
}