2.4 KiB
2.4 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
cf1111c1c12feddfaeb1bdef | JavaScript で整数の乱数を生成する | 1 | https://scrimba.com/c/cRn6bfr | 18186 | generate-random-whole-numbers-with-javascript |
--description--
小数の乱数を生成できるのはとても便利ですが、それを利用して整数の乱数を生成すればさらに便利です。
Math.random()
を使用して小数の乱数を生成する- その小数の乱数に
20
を掛ける - 別の関数
Math.floor()
を使用して小数点以下の端数を切り捨て、最も近い整数を得る
Math.random()
は決して 1
を返しません。そして切り捨てを行うため、20
を取得する可能性はありません。 この方法では 0
~ 19
の整数が得られます。
すべてをまとめると次のようなコードになります。
Math.floor(Math.random() * 20);
Math.random()
を呼び出して、その結果に 20 を掛け、その値を Math.floor()
関数に渡して端数を切り捨て、最も近い整数を求めています。
--instructions--
ここで紹介した方法で、0
~ 9
の整数の乱数を生成して返してください。
--hints--
randomWholeNum
の結果は整数である必要があります。
assert(
typeof randomWholeNum() === 'number' &&
(function () {
var r = randomWholeNum();
return Math.floor(r) === r;
})()
);
Math.random
を使用して、乱数を生成する必要があります。
assert(code.match(/Math.random/g).length >= 1);
Math.random
の結果に 10 を掛けて、0 ~ 9 の数値を求める必要があります。
assert(
code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) ||
code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g)
);
Math.floor
を使用して、小数点以下の端数を切り捨てる必要があります。
assert(code.match(/Math.floor/g).length >= 1);
--seed--
--after-user-code--
(function(){return randomWholeNum();})();
--seed-contents--
function randomWholeNum() {
// Only change code below this line
return Math.random();
}
--solutions--
function randomWholeNum() {
return Math.floor(Math.random() * 10);
}